- Home /
Duplicate Question
Updating GUI text
Hi I recently started a project that was single player. My player had a GUI text stating the engine %. This was done by having a GUI text gameobject in the hierarchy that was dragged to a public var box in the player controls script.
BUT....I then converted to multiplayer and this involved making my player a prefab as opposed to something already in the scene hierarchy.
Even though the text object is still dragged to the var box, the text no longer updates. Well, it does, but not on the screen! If I go to say 50% engine and stop the game, next time I start it, the engine will be at 50% even though I'm stationary!
So, why has making the player a prefab broken this? and how would I fix it? I've tried making the GUItext object a prefab too, but that doesn't work (and if I remember correctly, couldn't even drag it to the var box)
Cheers
Your question is not clear enough to indicate the exact solution, but your issue is a common one. Prefabs can be considered as blueprints for an object. They don't exist in a scene until they are Instantiated(). You cannot drag and drop from/to a scene object and a prefab to create links. Note you can create links through drag and drop within a prefab.
The solution will depend on which way you want to establish the links. If a prefab needs the link, then you would use GameObject.Find() or GameObject.FindWithTag(), possibly with GetComponent() to establish the link. Going the other way, the Instantiate() can save the reference and assign it to a variable.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://unitygems.com/script-interaction1/
http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
Still confused after searching "duplicate" questions & google. Was it really necessary to close this? I could have been clearer on the question had somebody asked for anything inparticular to show them to aid an answer.
Actually what should of happened is that this question should be rejected in the moderation queue with a request to 1) review some links on accessing other game objects, 2) restate your question, and 3) add your scripts to the question. Unfortunately the 'Send $$anonymous$$essage' feature of the moderation queue is broken. So my choice is either to just reject your question and have it disappear without you knowing why or to publish and close the question. Note that solving the accessing of variables on one game object from another is a very frequent question (many times most days). $$anonymous$$ost of those just disappear in moderation.
If after reading the materials and experimenting with the examples you are still stuck, resubmit your question with greater context and your scripts.
UnityGems appears to be down. I saw that a day or two ago. I hope it is not permanent since there is a lot of really good information on the site.
Follow this Question
Related Questions
GUI text blinking opacity 1 Answer
GUI Problem 2 Answers
Text Effects... 2 Answers
Multiple Cars not working 1 Answer