Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Find a variable

Discussion in 'Scripting' started by sheva, Jan 21, 2011.

  1. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    Hi guys,
    how can I find a varibale in an other scritp?
    For example, in my script "life" i wrote " life=...; "
    And now in one other script i've to reuse it ("....."=life;). How can I find the varible in the first script ? Thanks :)
     
  2. sccrstud92

    sccrstud92

    Joined:
    Jan 20, 2011
    Posts:
    145
    There are two ways you can do this.

    1) Declare "life" as a static variable.
    With this way. you can access the life variable using
    Code (csharp):
    1. var currentLife = LifeScript.life;
    2) Find the gameObject with the script that has the variable.
    With this method you access the life variable like this
    Code (csharp):
    1. var playerObject = GameObject.Find("player name here"); //obtain a reference to the object with the script attached
    2. var lifeScript = playerObject.GetComponent<LifeScript>(); //obtain a reference to the script itself
    3. var currentLife = lifeScript.life; //copy the value of the life variable to a local variable
     
  3. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    and if it's "" inaccessible due to its protection level "" ??? How can I get it ?
     
  4. sccrstud92

    sccrstud92

    Joined:
    Jan 20, 2011
    Posts:
    145
    I am guessing that you cannot access the variable because it is not declared public. By putting a public modifier in front of it, other scripts will be able to access it.
     
  5. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    If I write it in c# it doesn't work!!
    Code (csharp):
    1.  
    2. public GameObject playerObject;
    3. public Component lifeScirpt;
    4. public string currentLife;
    5.  
    6. playerObject = GameObject.Find("player");
    7. lifeScript = playerObject.GetComponent<LifeScript>();
    8. currentLife = lifeScript.life; //life is the LifeScritp's variable
    9.  
    Type `UnityEngine.Component' does not contain a definition for `life' and no extension method `currentSpeed' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
     
    Last edited: Jan 23, 2011
  6. sccrstud92

    sccrstud92

    Joined:
    Jan 20, 2011
    Posts:
    145
    Change the line "public Component lifeScript" to "public LifeScript lifeScript"

    And I don't think "strng" is spelled correctly.
     
  7. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    You also typo'd your "currentLife": public strng currentLife;
    Should be "string", not "strng".


    EDIT: and you typo'd "lifeScirpt". I guess this wasn't copy/pasted?
     
  8. Kemical

    Kemical

    Joined:
    Oct 4, 2010
    Posts:
    24
    When you call GameObject.GetComponent method in C# you must cast the result, see bellow :

    Code (csharp):
    1. public GameObject playerObject;
    2. public Component lifeScirpt;
    3. public string currentLife;
    4.  
    5. playerObject = GameObject.Find("player");
    6. lifeScript = playerObject.GetComponent<LifeScript>() as LifeScript;
    7. currentLife = lifeScript.life; //life is the LifeScritp's variable
     
  9. sccrstud92

    sccrstud92

    Joined:
    Jan 20, 2011
    Posts:
    145
    Actually Kemical, when using the generic form of GetComponent (i.e. GetComponent<Generic>()) you don't need to cast. You only need to cast when using the other form (GetComponent("component name")). And, in C# the cast is done like this:

    Code (csharp):
    1.  
    2. public GameObject playerObject;
    3. public LifeScript lifeScirpt;
    4. public string currentLife;
    5.  
    6. playerObject = GameObject.Find("player");
    7. lifeScript = playerObject.GetComponent<LifeScript>(); //lifeScript = (LifeScript) playerObject.GetComponent("LifeScript");
    8. currentLife = lifeScript.life; //life is the LifeScritp's variable
    9.  
     
  10. Kemical

    Kemical

    Joined:
    Oct 4, 2010
    Posts:
    24
    Sure the problem was in declaration not in cast (sorry) but thanks for your reply ;)

    About casting, for reference type (or nullable type) you can cast with keyword "as",

    Code (csharp):
    1.  
    2. MyType myVar = myVar2 as MyType
    3.  
    will do the same as

    Code (csharp):
    1.  
    2. MyType myVar = (MyType) myVar2
    3.  
     
  11. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    The subtle, but important differences between using "as" and casting with parenthesis is that:

    - You can't use "as" for a non-nullable type (structs, some generics).
    - If the cast fails (because you're casting a non-compatible type), then "as" will return null, whereas (TYPE) casting will throw an exception.

    In the case of Unity's GetComponent, there's really no reason to not use the generic version if you know the type you're working with at compile-time.
     
  12. sccrstud92

    sccrstud92

    Joined:
    Jan 20, 2011
    Posts:
    145
    Agreed
     
  13. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    guys, the problem is:

    Code (csharp):
    1.  
    2. public GameObject playerObject;
    3. public Component lifeScirpt;
    4. public string currentLife;
    5.  
    6. playerObject = GameObject.Find("player");
    7. lifeScript = playerObject.GetComponent<LifeScript>() as LifeScript;
    8. currentLife = lifeScript.life; // if the lifeScipt is a component the debug log says :
    9. // that Type `UnityEngine.Component' does not contain a
    10.  //definition for `life' and no extension method `life' of type `UnityEngine.Component'
    11. //could be found (are you missing a
    12.  //using directive or an assembly reference?)
    13.  
     
  14. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    You have a typo in your "lifeScirpt" declaration. Try fixing that.
     
  15. Kemical

    Kemical

    Joined:
    Oct 4, 2010
    Posts:
    24
    sccrstud92 give you the solution ^^

    do

    Code (csharp):
    1. public LifeScript lifeScirpt;
    instead of

    Code (csharp):
    1. public Component lifeScirpt;
    You don't need to cast the result (my mistake, sorry...) it's just that you defined your var lifeScript as a Component (And so there isn't any property "life" in UnityEngine.Component)
     
  16. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    Now it's work with this code:
    Code (csharp):
    1.  
    2. public GameObject playerObject;
    3. public LifeScript lifeScirpt;
    4. public string currentLife;
    5. public float life;
    6.  
    7. playerObject = GameObject.Find("player");
    8. lifeScript = playerObject.GetComponent<LifeScript>() as LifeScript;
    9. currentLife = lifeScript.life.ToString();
    10.  
    11.  
    Thank you guys :)