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 flojer0 · May 31, 2012 at 06:49 AM · updatefunctiondelay

Function in Update delaying involuntarily

I have a section of code that I wanted to move out of update to prepare some organization for the future. Inside of update the segment runs as expected, but once I place the code segment into a separate function and call it I won't get a response until several seconds after it was suppose to trigger.

I literally copy and pasted between the two and there is only an 'if' statement in the function so far.

 function Update () {
     var forward : Vector3 = transform.TransformDirection(Vector3.forward);
     var back : Vector3 = transform.TransformDirection(Vector3.back);
     Debug.DrawRay (transform.position, forward, Color.white);
     
     if (Physics.Raycast(transform.position, forward, hit, 1))
         if (hit.collider.gameObject.name ==  "Player")
         {
             var test = hit.collider.gameObject.GetComponent(ThirdPersonController);
             if (test.travelAxisCurrent == 4)
                 if (!Physics.Raycast(transform.position, back, hit, 1))
                 {
                     AssignDestination();
                 }
         }
         else{}//was planning on using this else, haven't yet.
         
     var speed = direction * Time.deltaTime;
     transform.position.z += speed;
     
     //AtDestination();
     if (transform.position.z + speed < destinationZ)
     {    
         transform.position.z = destinationZ;
         direction = 0;
     }
 }
 
 function AtDestination()
 {
     if (transform.position.z + speed < destinationZ)
     {    
         transform.position.z = destinationZ;
         direction = 0;
     }
 }


EDIT: as requsted, all relevant code. As is with the conditional in Update it runs perfectly, but if I comment out the conditional and un-comment the call the reaction is delayed acouple of seconds. I did try to figure this out but I'm at a complete loss as to why putting an if statement in a method would cause such behavior. And sorry for the readability of the function, I can't get the code formatting to cooperate.

EDIT 2: Finally found the issue. It skipped my mind that speed isn't global. I really should stop working with global variables. So now my assumption is that once the block was pushed it just kept going until the block passed some arbitrary amount that was where the new un-initialized speed variable was. Thanks, and I guess sorry I bugged you all with something so noobish.

Comment
Add comment · Show 6
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 hijinxbassist · May 31, 2012 at 06:53 AM 0
Share

Are you calling that function from Update()?

 function NewFunction()
 {
     Debug.Log("Running New Function");
 }
 function Update()
 {
     NewFunction();
 }

This will call New Function every frame w/o any delay. What is in your if statement?

avatar image flojer0 · May 31, 2012 at 07:17 AM 0
Share
 if (transform.position.z + speed < destinationZ)
 {    
     transform.position.z = destinationZ;
     direction = 0;
 }

as I said, the code works perfectly fine when placed in update, but once it's moved to a function I get problems.

avatar image hijinxbassist · May 31, 2012 at 09:47 AM 0
Share

@flojer0 Is there something blocking the call of the function(a conditional)? When does the function get called?

avatar image Wolfram · May 31, 2012 at 09:50 AM 1
Share

Please edit your question to include the whole script, there must be a problem somewhere in the code.

avatar image Bunny83 · May 31, 2012 at 08:15 PM 0
Share

Yep do like @Wolfram said. It's impossible that this caused any problems if that's the only code in your function.

Ask specific and detailed questions. That doesn't mean you have to include 1000 lines of code. Only include the relevant part. If it's still too much code, try to figure out what part is causing the problem. You can only move a part of your code into the function and see how it goes.

You should try to spot the error on your own before you post a question here. This should be a knowledge database and not a human crystal-ball that solve your personal issues for free.

If you have an issue that could happen to others and you couldn't find a solution, ask a question, but take the time to describe your problem because at the moment you actualy waste our time trying to figuring out your problem ;)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 02, 2012 at 09:04 AM

What is can directly see is that you defined a speed variable inside of update. That means this speed variable is only available in Update. In your AtDestination function you also use a speed variable, but it's definately not the same as the one used in Update.

I guess you have a speed variable in your class / script itself and want to use this instead of creating a local variable in Update. So just remove the "var" in front of speed and you're good to go.

 speed = direction * Time.deltaTime;

edit
Next thing that is a bit strange is you check only if the position along the world axis is below your destinationZ. Do you only move backwards or what's the purpose of the check?

Comment
Add comment · Show 1 · 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 flojer0 · Jun 02, 2012 at 06:22 PM 0
Share

As I stated, only you put it better. The variable is local to update at the moment, I just haven't removed all the bad practices I let myself get while learning unity and javascript(unity script).

If you want to know for curiosities sake I'm building a game that involves pushing blocks around, and I wish to keep the player and the blocks on a grid pattern. This is the start of keeping the block I'm testing on moving one unit at a time unless I'm still pushing it. So only one direction is tested.

avatar image
0

Answer by hirenkacha · May 31, 2012 at 06:52 AM

you will have to use coroutine in update (). for example click here.

Comment
Add comment · Show 5 · 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 hijinxbassist · May 31, 2012 at 06:54 AM 2
Share

You cannot use a coroutine in Update(). http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html>Look at the bottom of this page.

avatar image hirenkacha · May 31, 2012 at 07:31 AM 0
Share

ohh.. so i will have to use this in a seprate function. But can i use yeild waitforseconds?

avatar image hijinxbassist · May 31, 2012 at 09:41 AM 0
Share

@hirenkacha No coroutine can be used inside of Update(). You can use StartCoroutine to start a function that can as stated at the bottom of the link i posted in my prev comment. You cannot delay the Update call.

avatar image hirenkacha · May 31, 2012 at 04:40 PM 0
Share

@hijinxbassist Thanx friend for correcting me... it will help me in my game too..

avatar image Bunny83 · May 31, 2012 at 10:03 PM 0
Share

@hijinxbassist: You should watch your words because you can use coroutines in Update. Update itself can't be a coroutine so you can't use yield in Update, but you can start coroutines in Update.

In UnityScript you don't have to use StartCoroutine(), that happens behind the scenes. If you just call a coroutine like a normal function, Unity will call StartCoroutine for you.

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

8 People are following this question.

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

Related Questions

How to create a delay in a function being called in update? 2 Answers

can i delay the script in the 'function update'? 2 Answers

How to Update a score count going up and down 1 Answer

Update Function Work Around Question 2 Answers

Weird delay on Tween 1 Answer


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