- Home /
Reference variable in other script
Hello,
I have an object that the player must hit, the object needs to be hit 3 times for something to happen, I want to be able to have several of these objects in my scene.
The problem I am having is referencing this hit value from another script.
The object I am hitting has a script to detect collisions, while another object has the scripts that are controlling its behaviour. I need to be able to get the hit counter from the collisions script.
Answer by Eric5h5 · Aug 01, 2010 at 11:53 AM
This is covered in the docs: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I receive a NullReferenceException when I try to run the game, I am using this code;
maledictorHealth = transform.Find("$$anonymous$$aledictor").GetComponent($$anonymous$$aledictorHits).hits;
Is the script on '$$anonymous$$aledictor' really called $$anonymous$$aledictorHits
?
$$anonymous$$ake sure an object with the named, and capitalized "$$anonymous$$aledictor" actually exists when the line is run. If this is true, then try running:
if (!transform.Find("$$anonymous$$aledictor")) { Debug.LogAlert("$$anonymous$$aledictor was not found!"); }
If you get the warning "$$anonymous$$aledictor was not found" in the log, then you may have mispelled $$anonymous$$aledictor. If you don't get that error, try it again, but replace !transform.Find("$$anonymous$$aledictor") with transform.Find("$$anonymous$$aledictor").GetComponent($$anonymous$$aledictorHits). If you get a not found, then you may have misspelled the script name.
Ok, so I have tried to work with this script to get a variable out of another script...
Graphics is the object GUIButtons is the script test is the variable in the script I want to get the value of
function Update () { var test = transform.Find("Graphics").GetComponent(GUIButtons).test; //Then here I would like to print the value it finds for the variable "test" }
nothing I am doing is working. ANY help would be great!
Post this as a new question - it's confusing to post a question as an answer to another question!
Answer by ravindra123 · Nov 23, 2012 at 07:27 AM
make that variable as public static. in another script acees though classname.varname
Your answer
Follow this Question
Related Questions
[UNSOLVED] Object reference not set to an instance of an object 0 Answers
How to reference GameObject in Awake or Start 2 Answers
How to tell if 2 blocks are next to each other in a 2d game? 1 Answer
OnCollisionEnter Collision not detected? 1 Answer
how to pass an int to a function by reference in Javascript ? (as an InOut parameter) 1 Answer