- Home /
How do I reference variables from other class files?
I am trying to reference a boolean value from the javascript file: ItemBools. In ItemBools, there is a public variable named isAxeFound. In another class called AxeCollisionScript, I need to change the value of isAxeFound that is stored within the javascript ItemsBool. I tried referencing the variable by using the line: ItemsBool.isAxeFound = true; , but that doesn't work. If you need to see code, I can submit it, but I thought it was simple enough to understand. Thanks!
Answer by WhiteMaegi · Dec 04, 2013 at 08:22 PM
If both of these scripts are attached to the same game object, you can use GetComponent(). So from within your AxeCollisionScript, add this line:
gameObject.GetComponent(ItemBools).isAxeFound = true;
If these scripts are not attached to the same object, I'm going to need more information.
Also, check this doc for more details: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
Thanks, you got me on the right path! You actually showed me how to single out attributes of a component :) And yes, the scripts were NOT attached, I was able to figure out that if you say:
GameObject.Find("").GetComponent().isAxeFound = true;
Worked great!