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 ozturkcompany · Nov 16, 2013 at 10:01 AM · updatefunctionawake

What is the difference Between Awake and Update Function?

Hi, i am kind of confused here.Maybe it is an odd question but i need to ask cause i am getting different results. So here is my basic deltaPosition code that detects the delta position of an object during last frame.There are two different results and that is where i get confused.When i define the variables in awake function my delta result in Vector3.zero but when i define my variables in a custom function that will be checked in update function every frame,everything is fine as well as correct delta values.Pls tell me what is wrong with it,why everything gets messy while i define the variables in awake function???

Thank you!

 #pragma strict
 var firstPos : Vector3;
 var secPos : Vector3;
 var deltaPos : Vector3;
 var target : Transform;
 
 function Update () {
 
 target.Translate(0.1, -0.2, 0);
 
 DeltaPos ();
 
 }
 
 function DeltaPos () {
 
 firstPos = target.position;
 yield;
 secPos = target.position;
 deltaPos = secPos - firstPos;
 
 }
 
 #pragma strict
 var target : Transform;
 
 function Update () {
 
 target.Translate(0.1, -0.2, 0);
 
 DeltaPos ();
 
 }
 
 function DeltaPos () {
 
 var firstPos = target.position;
 yield;
 var secPos = target.position;
 var deltaPos = secPos - firstPos;
 
 }

First code results the delta value in (0,0,0) and the second code results in the correct delta value which is (0.1, -0.2, 0).

What is wrong??

CORRECT DELTA VALUES!

alt text

WRONG DELTA VALUES!

alt text

Here is the scene if you want to check it yourself;

http://s3.dosya.tc/server13/MV1hkN/NewUnityProject.rar.html

untitled.jpg (262.4 kB)
untitled2.jpg (273.6 kB)
Comment
Add comment · Show 4
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 VioKyma · Nov 16, 2013 at 10:35 AM 0
Share

What is the difference between these scripts. The only difference I can see is that the first has three unused Vector3s defined. (I am assu$$anonymous$$g 'firstPos' is actually 'first'?)

avatar image ffxz7ff · Nov 16, 2013 at 10:41 AM 0
Share

Where are your variables declared? It isn't obvious.

avatar image ozturkcompany · Nov 16, 2013 at 10:43 AM 0
Share

Oh i am sorry.I edited it now.Now would you take a look at it again?

avatar image ozturkcompany · Nov 16, 2013 at 10:44 AM 0
Share

Vio$$anonymous$$yma you are right! I mis typed the variables.Sorry about it.

2 Replies

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

Answer by stardust · Nov 16, 2013 at 12:21 PM

For your question about the difference between Awake and Update, you could read through the description in the Scripting reference, for example the Awake Function: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Awake.html

I would recommend to read this question, where runevision explains the difference between Awake, Start, Update and so on: http://answers.unity3d.com/questions/10189/what-is-the-general-use-of-awake-start-update-fixe.html

But your problem actually is caused because you call a coroutine which is executed over several frames (in your case two) in every frame via the Update function.

 #pragma strict
 var firstPos : Vector3;
 var secPos : Vector3;
 var deltaPos : Vector3;
 var target : Transform;
  
 function Update () {
  
 target.Translate(0.1, -0.2, 0);
  
 DeltaPos ();
  
 }
  
 function DeltaPos () {
  
 firstPos = target.position;
 yield;
 secPos = target.position;
 deltaPos = secPos - firstPos;
  
 }

In your first code example your variables are instance variables, which means, every function in your script can access it. So what happens is:

In frame 1 you call the function DeltaPos. In DeltaPos the targets position gets stored in firstPos. Then DeltaPos waits for the next frame. In frame 2 in the Update function you again call DeltaPos. So you now have two DeltaPos running. DeltaPos2 now stores the targets position of frame 2 in firstPos, while DeltaPos1 runs its second part and compares firstPos (already overwritten by DeltaPos2) with secPos (the targets position in frame 2) which is the same and therefore equals 0.

 #pragma strict
 var target : Transform;
  
 function Update () {
  
 target.Translate(0.1, -0.2, 0);
  
 DeltaPos ();
  
 }
  
 function DeltaPos () {
  
 var firstPos = target.position;
 yield;
 var secPos = target.position;
 var deltaPos = secPos - firstPos;
  
 }

In your second example the variables firstPos, secPos and deltaPos are declared in DeltaPos itself, which means only DeltaPos itself can access it. This will happen in your script:

In frame 1 the function DeltaPos is called. It then stores the targets position in his own firstPos(1). DeltaPos now waits for frame 2. In frame 2 you call another DeltaPos. DeltaPos2 then stores the targets position in frame 2 in its own firstPos(2). DeltaPos1 compares now the value in his firstPos(1), with the targets position in frame 2. So you have two firstPos variable, one for each DeltaPos you called.

Because you call DeltaPos in every frame anyways, you don't need the yield. I would recommend you to change your code to something like this:

 #pragma strict
 var firstPos: Vector3;
 var secPos: Vector3;
 var deltaPos: Vector3;
 var target: Transform;
 
 function Start () {
   firstPos= target.position;
 }
 
 function Update () {
   target.Translate(0.1, -0.2, 0);
   DeltaPos();
 }
 
 function DeltaPos() {
   secPos= target.position;
   deltaPos= secPos- firstPos;
 
   firstPos= target.position;
 }

At first you store the targets position in the Start function for the first time. DeltaPos now all does in one Frame. When called, it takes firstPos stored in the last Frame, compares it with the targets position in the current frame. When it has done all the calculations, in your case if you have deltaPos, it overwrites firstPos, as a preparation for the next frame.

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 ozturkcompany · Nov 16, 2013 at 12:38 PM 0
Share

Great explanation! Thank you a lot man for the time of your effort writing it.Im glad!

avatar image
0

Answer by ffxz7ff · Nov 16, 2013 at 10:44 AM

What is the difference Between Awake and Update Function?

As far as I know, Awake() gets called once when the object is created, and Update() runs once per frame. But I think your code example doesn't show the problem so far.

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 ozturkcompany · Nov 16, 2013 at 10:47 AM 0
Share

I edited the code now, i mis-typed the variables now it is correct. $$anonymous$$y problem is that when i declare the variables in Awake function delta value is result in a wrong way which is Vector3.zero. But when i declare my variables in a custom function it shows the correct delta values which is (0.1, -0.2, 0). $$anonymous$$y question is why these two results are different? What happened when i defined my variable in awake function? Why it show the wrong values?

avatar image ffxz7ff · Nov 16, 2013 at 10:49 AM 1
Share

why everything gets messy while i define the variables in awake function???

If you actually declared the variables inside the Awake() function, then they should only be accessible within that function. Declaring your variables as part of the class might fix your problem.

avatar image ozturkcompany · Nov 16, 2013 at 10:54 AM 0
Share

Hmm but we can access that variables in any function not just in the awake function? When i top at top of the script like var a : float; I can access and change that a variable in any function inside. I don't understand it why these 2 scripts has different results? The only difference is that one of the script variables defined at top of the script which is awake and in the other script it was defined in a custom fucntion. The only thing is that the place of the variables where it is declared is different. Both of the code is actually doing the same thing.

avatar image ffxz7ff · Nov 16, 2013 at 11:04 AM 0
Share

Do you override firstPos, secondPos and deltaPos anywhere else in the script? Do you perhaps declare them twice, in the class and in Awake() function?

avatar image ozturkcompany · Nov 16, 2013 at 11:13 AM 0
Share

No i don't override or change them anywhere else in the script.I can show you the scene file if anyone is interested?

http://s3.dosya.tc/server13/$$anonymous$$V1hkN/NewUnityProject.rar.html

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers

Update increment error (2 + 1 = 0?) 1 Answer

How do i delay function Update 3 Answers

Help with If statement INSIDE if statement!! 2 Answers

on mouse enter without function update 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