- Home /
Access a variable from another script on another object
This has been asked a lot and I find many things simply by searching, but I can't workout a simple single code I have to add... I have a script attached to a count down timer and a script attached to the player. Once the counter reaches zero it has t access the player script check the changeHealth variable and do things accordingly. It's just a few lines, but I cannot find 'em.
Answer by meat5000 · Oct 20, 2013 at 10:44 AM
Declare a new object variable.
GameObject.Find the object with the script you wish to access. Place it in the object variable you created.
Create a variable of the type you wish to access.
Use GetComponent to access the script and variable you wish to access and place the result in the variable you created.
It works with object Transform well as GameObject.
Your answer
Follow this Question
Related Questions
Create Array Accessible By Any Script 2 Answers
Accessing a variable inside a class inside other script... 2 Answers
how do i change a bool from another script 1 Answer
Access UI in scene from an instance prefab 1 Answer
Restart button 7 Answers