- Home /
Update() doesnt call method or modify values
I have a script attached to a prefab, but when i instantiate it, it doesn't seem to execute update(). Here is my code from justpasteit.
http://justpaste.it/BuilderScript
I have my code store Time.time into a variable called curTime, but it doesn't seem to do it, it stays at 0. Nothing else modifies this value. 0 is what i initialized it to tho.
When i change curTime to initialize to 5, it shows up as zero in the editor when i play my game. I also changed my variable to private, then to public again (to make sure the inspector isn't overriding the value in the script).
Another problem is that a function that prints a string to a guiText gameobject doesn't seem to be called. It calls fine in all other methods, but not update. (and there isn't a null reference). What could cause this?
I see that you set the variable to Time.time
, but do you do anything with it, or just set it.
for future reference don't give the entire script if you just want us to look at one variable (unless you want a full review of that script). try to give us just the parts that concern the problem.
I don't know if this may be causing your problem, but calling GameObject.Find at Awake isn't a good idea: Awake is called when the object is instantiated, and the targets searched may not have been created yet. GameObject.Find should be called at Start, when the scene is complete.
I will do something with Time.time later, but i noticed it snot being changed, so why would iu write the rest of my script if this part doesnt work? Seems like bad debug practice to me. I write my scripts puiece by piece and then test each section a si write them to make sure they work. Similar to blackbox or unit testing...
Extra info, i put in another useless int var (because my debug print isn't working) and my update seems to execute once. I put "public int test = 10;" in class as class member, and then i put test++; inside update and it only goes to 11. it only executes once. But i put this code after my debug output statement method calls and it doesn't print to my debug obj, even though my int is being incremented (so execution is getting over the method call lines). Why is this?! I'm getting a little irate at unity right now because update() is called every frame but its not doing what i tell it...
Well, since the question isn't clear and detailed we can't post a clear answer. This is more a disucssion (which shouldn't happen) to figure out what's even wrong.
@lord-richard-2.html: If it's a prefab, can you export it and link the unitypackage here? Don't forget the code dependencies.
If not, well this is a dark end i guess...
Answer by 1337GameDev · May 25, 2012 at 06:34 PM
I found the answer. It is from unity not being able to handle "no touches" when building for a mobile touch device (iOS / android) and throws a nullreference whenever you reference a touch without checking before hand if touchCount > 0. Unity reported a null reference in my camera controller script (but it always did this and other updates methods would be called just fine) but for some reason unity kept skipping my builder and buildqueue update method calls. When i fixed this error: Input.GetTouch(0).position, my update calls were then being completed more than the first frame for my builder stuff. I am starting to hate unity and how it handles mobile and exceptions but doesnt tell you any extra info, we just have to know. I assumed since this exception happene din main it would just skip my cameraController script's update(), but that apparently was the case here. I wish they would improve debugging.
I could have caught this with unity remote, but their latest version for android is broke and doesn't work with the latest unity and they dont seem to care to update it, which is quite sad.
So basically you posted the totally wrong code since you had other errors as well. Also it's not Unity that isn't able to handle "no touches", it's you ;)
The examples in the scripting reference do check touchCount before accessing a Touch. Actually you should do this in any case where you access an array or index based datastructure. You always have to be sure your index is in a valid range.
There are some exceptions like the Collision struct. The contacts will always contain at least one element, otherwise there is no collision at all ;) Anyway, checking the array length doesn't hurt and saves you from any potential error that might raise an exception at a different place.
but unity didnt follow its "rule" of encountering an exception and just skipping that method. it skipped just these updates, but other updates were fine. Which threw me off and made me assume i had another error in the code i didnt account for. Those "exceptions for touch" only occur on my camera script and happen only in editor play test mode. It also had no other effect on anything else and was "contained" to just the camera script, which is alright in my case. Why would unity not execute update() in other scripts that are fine? Or does unity just get all messed up from exceptions?
Always pay attention to Null Reference Exceptions: in 99.99% of the cases, the cause is between the chair and the keyboard!