- Home /
Variables in Instantiated Prefab Scripts and OnGui
Hi, I learned a lot from Unity Answers and spent my whole weekend searching for a usable one as I don't like posting ones myself. As I did not find anything suitable I do have to make that step:
My problem is that I have a prefab with a script as component. I Instantiate that prefab and while instantiating I change the variable of that script to a certain value
var Script = SomeArray[SomeInt].GetComponent(SpecificScript);
Script.CurID = GameSettings.BuildingID;
Now the instantiated object script uses that CurID
variable as wished. The problem I get is that the OnGUI()
function in the prefab only uses the CurID
of the first instantiated object. Means if I use a menu of the second instantiated object the OnGUI()
of this function uses the CurID of 1
. But when I pause the game and look in the inspector, the CurID
variable of the cloned object is 2
as it should be.
What am I doing wrong? Or is this a known problem and is there a workaround?
I hope you got a sense of what my problem is, it is a little bit complicated thank you in advance for your help!
The script that has the OnGUI function and the SpecificScript are both components of the same prefab? Also what data type is the array? GameObjects?
Do you have two objects with the same SpecificScript comoponent? It's possible that they are both trying to display their GUI at the same time.
$$anonymous$$ake sure you aren't using static variables in your SpecificScript class that may be being shared between all instances.
And finally, put a 'Debug.Log(CurID);' in your OnGUI code to double-check the value then look in the console when you hit Play. You may find something important.
@ Atrius: I use the onGUI() of the instantiated prefab and the Array is a GameObject Array, yes.
@ IgnorancelsBliss: The Gui is displayed by asking for a static variable, so you mean that everytime I click on any instantiated prefab I only call the $$anonymous$$enu of the first prefab and that's why I don't have the desired CurID? That could be the solution, so I better put the OnGUI part into another script I guess...oh boy that's going to be complicated...
Thank you for your answers, I will try outsourcing the function tomorrow and then report back if this is solved!
Answer by oneWaveStudios · Apr 24, 2012 at 02:17 PM
IgnorancelsBliss made the right suggestion, I used the GUI component with a static variable, so everytime an object was clicked it would show up the GUI of the first object, no matter what is number was, thank you very much! --@admin: Solved