- Home /
How to access a game object from a Prefab?
Hello
i am making a game (using C#) which spawn enemies, there is 2 game objects (Spawner and HP Bar) and 1 Prefab (Enemies)
i made a script which make the enemies (prefab) lose HP (Hp Bar Scale down)
however i cant access the game object (Hp Bar) from the prefab (enemies) unless i make the HpBar a prefab but that doesnt help me.
so is there a way to access a game object from a Prefab?
Thank you very much
Unclear what you are asking. Typically a prefab will find game objects it needs to access when it is created. GameObject.Find() and GameObject.FindWithTag() are the two most common ways a prefab will find game objects.
sry for unclear question :(
but your answer helped me :D thank you
1 more question please :D
how can i access a GUI Text?
If the GUIText is on the same game object, you can just use 'guiText'.
guiText.text = "Some string";
If on another object, then you can do:
otherObject.guiText.text = "Some text";
Note that under the hood, Unity is doing a GetComponent() call for you.
Your answer
