Why won't my property value update to the new value I assign in a different script?
So after struggling with this for several days, I am totally frustrated and really don't understand what is going on. All i want to do is have a script assign an ID number to a game object each time it spawns. To do this, I pass a value either through a property or through a public function. However, no matter what I do, the value in the target script never changes from 0. Here is the code:
In script 1: private void AssignObject(GameObject obj) { activeObject = obj; if (activeObject.GetComponent()) { movementScript = activeObject.GetComponent(); movementScript.ChangeID(activeID); } }
Script 2, the target script: public void ChangeID(int newID) { GummiID = newID; }
I have debugged this thing over and over again. It shows in the first script that movementScript.GummiID = the new value, yet in the actual script it shows it is still 0! Stack trace shows no duplicate scripts. The logic flow is exactly as I want it. I looked up online multiple pages of this and I am certain I have the correct syntax. So why doesn't the target property update when I call the function?
Sorry, I can't seem to get code blocks working on here.
Answer by TarwaterPV · Oct 24, 2018 at 07:24 PM
Ok, I figured it out. The execution order of the events were occurring at the same time instead of one after the other in a specific order so I went ahead and corrected it by having the spawner instead of the button assign the ID of the spawned object instead of it occurring at the same time as the spawn event.
Your answer
Follow this Question
Related Questions
Can someone explain this to me? Player myPlayer = new Player(); 1 Answer
saving objects with properties in a c# list 1 Answer
How do I add a property to built-in shader? 0 Answers
GameObject variable not holding it's value 0 Answers
Multiple variables in a function? Like function test (number : float , number1 : float) 0 Answers