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
There are two ways you can do this.
- Declare “life” as a static variable.
With this way. you can access the life variable using
var currentLife = LifeScript.life;
- Find the gameObject with the script that has the variable.
With this method you access the life variable like this
var playerObject = GameObject.Find("player name here"); //obtain a reference to the object with the script attached
var lifeScript = playerObject.GetComponent<LifeScript>(); //obtain a reference to the script itself
var currentLife = lifeScript.life; //copy the value of the life variable to a local variable
and if it’s “” inaccessible due to its protection level “” ??? How can I get it ?
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.
If I write it in c# it doesn’t work!!
public GameObject playerObject;
public Component lifeScirpt;
public string currentLife;
playerObject = GameObject.Find("player");
lifeScript = playerObject.GetComponent<LifeScript>();
currentLife = lifeScript.life; //life is the LifeScritp's variable
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?)
Change the line “public Component lifeScript” to “public LifeScript lifeScript”
And I don’t think “strng” is spelled correctly.
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?
When you call GameObject.GetComponent method in C# you must cast the result, see bellow :
public GameObject playerObject;
public Component lifeScirpt;
public string currentLife;
playerObject = GameObject.Find("player");
lifeScript = playerObject.GetComponent<LifeScript>() as LifeScript;
currentLife = lifeScript.life; //life is the LifeScritp's variable
Actually Kemical, when using the generic form of GetComponent (i.e. GetComponent()) 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:
public GameObject playerObject;
public LifeScript lifeScirpt;
public string currentLife;
playerObject = GameObject.Find("player");
lifeScript = playerObject.GetComponent<LifeScript>(); //lifeScript = (LifeScript) playerObject.GetComponent("LifeScript");
currentLife = lifeScript.life; //life is the LifeScritp's variable
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”,
MyType myVar = myVar2 as MyType
will do the same as
MyType myVar = (MyType) myVar2
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.
Agreed
guys, the problem is:
public GameObject playerObject;
public Component lifeScirpt;
public string currentLife;
playerObject = GameObject.Find("player");
lifeScript = playerObject.GetComponent<LifeScript>() as LifeScript;
currentLife = lifeScript.life; // if the lifeScipt is a component the debug log says :
// that Type `UnityEngine.Component' does not contain a
//definition for `life' and no extension method `life' of type `UnityEngine.Component'
//could be found (are you missing a
//using directive or an assembly reference?)
You have a typo in your “lifeScirpt” declaration. Try fixing that.
sccrstud92 give you the solution ^^
do
public LifeScript lifeScirpt;
instead of
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)
Now it’s work with this code:
public GameObject playerObject;
public LifeScript lifeScirpt;
public string currentLife;
public float life;
playerObject = GameObject.Find("player");
lifeScript = playerObject.GetComponent<LifeScript>() as LifeScript;
currentLife = lifeScript.life.ToString();
Thank you guys