I have a prefab which I am using in the game as the enemy. Now - I have a variable attach to that which transforms when it gets a hit. This variable is accessed by an empty gameobject which is responsible to display it using OnGUI() function.
If you have to look into the code, e_distance is the variable which is inherited in another emptygameobject in the OnGUI() function.
This script is attached to the "enemy" prefab and that prefab is drag onto the empty gameobject which is referencing this script. To be clear again one is a Prefab and other is an gameobject on the scene view.
As you might see on the code, I make an attempt to print it on the screen. When I run the level, the value that prints on the console is accurate but on the GUI , it is just zero (which is the initial value). Moreover when I did trace a bit - I happen to find that the variable e_distance on the inspecter pane of "enemy" prefab remains zero. i.e. the value in the prefab does not change.
I am not sure where I have went wrong. I would be happy and would appreciate if someone could help me on this.
THANK YOU in advance.
asked Dec 09 '11 at 05:26 AM
just Instantiate another variable of type gameobject. for eg:-
then Instantiate the prefab to it.
now, you can acces the variable from these game objects.
I hope you have got this answer long before, but I am writing this down coz the anyone with same problem is supposed to end here. Syclamoth has given the answer but a beginner like me took a lot of time to grasp what he said. So I am answering here even though its late.
answered Mar 20 '12 at 02:42 PM
The problem is that while the enemy instance changes, the value in the prefab does not. In fact, the prefab doesn't even execute 'Update', so you're getting the GUI value out of one object, and the Debug.Log value from a totally different object which doesn't even know about the prefab it was instantiated from.
Basically, when you instantiate the prefab (in the usual way), you need to pass a reference to the instance that was created to the GUI object that keeps track of it. The prefab never moves, because it is a 'virtual' object that can't act on the game world without first being instantiated, but on the other hand can never be modified or destroyed (and so is sure to be a predictable starting point for other objects). Think about it the same way you would the relationship between a class and an object of that class.
answered Dec 09 '11 at 07:00 AM