Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 1337GameDev · May 23, 2012 at 10:26 PM · variableupdatefunctionpublic

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?

Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kevin_C · May 23, 2012 at 10:45 PM 0
Share

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.

avatar image aldonaletto · May 23, 2012 at 10:57 PM 3
Share

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.

avatar image 1337GameDev · May 23, 2012 at 11:43 PM 0
Share

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...

avatar image 1337GameDev · May 24, 2012 at 01:19 AM 0
Share

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...

avatar image Bunny83 · May 24, 2012 at 10:14 AM 1
Share

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...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 26, 2012 at 03:05 AM 2
Share

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.

avatar image 1337GameDev · May 26, 2012 at 06:37 AM 0
Share

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?

avatar image aldonaletto · May 26, 2012 at 06:40 AM 0
Share

Always pay attention to Null Reference Exceptions: in 99.99% of the cases, the cause is between the chair and the keyboard!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

summing up items price in runtime 1 Answer

How to prevent object (bullet) from instantiating twice ? (It instantiates once for tapping the screen, and once more when beginning to hold touch on screen) ? 1 Answer

Variable Doesn't Update in "Update" function 0 Answers

Access a variable from another script in update function 1 Answer

Having code fire once in update function? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges