Search Unity

classes and variables visible to all scripts

Discussion in 'Scripting' started by dansav, May 1, 2008.

  1. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    If I create a class in one script is the class recognized by all scripts? If not how do I make it so.

    If I make a variable in one script. How do I make it visible to all scripts.
    Is there some doc about variable scope and public and private and whether it's inside or outside a function and all that??

    thanks,

    Dan
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep.

    You can use a static variable:

    Script1.js:
    Code (csharp):
    1. static var something = 2;
    Script2.js:
    Code (csharp):
    1. Script1.something = 4;
    As long as it's a public variable, though, you can still access it from other scripts using GetComponent. Static variables are a little more convenient, but sometimes that's not what you want.

    Not sure about docs, but in Unity Javascript:

    Code (csharp):
    1. var something = 2; // public, global scope
    2. private var somethingElse = 3; // private, global scope
    3.  
    4. function Start () {
    5.    var foo = 1; // local scope
    6.    for (var i : int = 0; i < 10; i++) {} // local scope for function but not this loop
    7.    for (var i : float = 0.0; i < 10.0; i++) {} // error, i is already defined in this scope
    8. }
    --Eric