|
Hello, In my hierarchy I have a tank, within that tank (as a child) I have a health bar (plane). Also in my hierarchy, I have a game empty with a script attached to it called 'Controller'. Is there a way for my tank health bar script (which is attached to the health bar) to grab a variable from the Controller script? Hierarchy:
I don't want to use static variables. I was looking at the GetComponent, but that only seems to work when grabbing a variable on the object, or within (child). Please help, Thanks
(comments are locked)
|
|
You can either use GameObject.Find and GetComponent, or FindObjectOfType. If there's just one Controller script, then FindObjectOfType is more direct.
(comments are locked)
|
|
It's somewhat tricky. In the HealthBarScript do the following: I don't know if the var ctrl can be defined outside functions and initialised by Start(), but if it can be done it will optimize runtime performance.
(comments are locked)
|
