Jump to content
  • Advertisement
Sign in to follow this  
twinspectre

Csharp Unity Awake() function

This topic is 585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm new to Unity and Programming in general, but I learned the basic of Csharp.

I wanted to ask you all and I hope I will get a more constructive answer, I tried to ask this question to other forums and I get the same repsonse.

 

I understand that Awake start before the Start() functions and following Start() We have Update() and so on.

 

I would like to know Why We need to use Awake, what I should insert in Awake functions, how it works, what's the deal with Awake, well I would like to understand everything about Awake()

 

I've read the Unity Documentation about awake, I saw videos about awake and it seems some people don't understand what awake is, because all they do is repeating like a parrot

 

You can post a script where you can help me to understand this topic

Share this post


Link to post
Share on other sites
Advertisement

Your question reads like "please write a custom manual for me about awake".

 

I don't know what the function does, but likely it does a lot, or people wouldn't be confused by it.

Perhaps you should start with telling us what you think it does, and point out where you're confused. That gives a lot more focus on the precise areas where you're wrong or unsure of. It also avoids us spending useless time on explaining loads of stuff you already guessed correctly.

Share this post


Link to post
Share on other sites

everything I read or watch videos about Awake, I see a parrot (I don't want to be offensive)

I don't have a script, but I'm confused on what's the deal with Awake, how it can benefit, what I can insert in the awake function, is a topic I want to understand very well

Share this post


Link to post
Share on other sites
Awake is only a convenient way to initialize stuff you need *before* the start method.
That's all there's to it.

Let's say you have 2 scripts doing stuff at start, but script A needs something that script B initializes, the best way to guarantee that is you put B code in the awake instead of start.

Hope it helps

Share this post


Link to post
Share on other sites

If everyone says roughly the same, why does that mean they don't understand it?

 

For example, I'm sure there's a lot of textbooks saying 1 + 2 = 3 -- does that mean none of them get it, and that they're only parroting information?

---

Awake is basically the script's constructor. Here, you essentially initialize the script so all the variables are what they should be when the script begins its life.

 

However, you the order in which Awake is called on the various objects is random. You don't know that scriptA's Awake runs before scriptB's Awake.

That means you cannot in scriptA's Awake assign or do anything that depends on scriptB's Awake function having being called. (And vice versa.)

 

Have initialization code that doesn't depend on other objects in Awake.

 

Start is executed after all Awake functions have been executed.

That means that in scriptA's Start function, you can always refer to everything setup in scriptB's Awake.

 

I seem to remember there being some exceptions to this, regarding network handling and such, but in general:

Awake() --> Internal setup. Does not rely on what other objects do in their Awake().

Start() --> External setup. Here you can safely rely on the fact that other objects have had their Awake() (internal setup) completed.

Share this post


Link to post
Share on other sites

with B code you mean script B, right?

I meant if you move the code you wrote in the start method to the awake method, you will be sure that it will get executed before the start method of all the other scripts. Edited by nesdavid

Share this post


Link to post
Share on other sites

Awake gets called immediatly after the gameobject gets created. I think that Start excecutes immediatly before the first update, and so an unactivated object will not excecute Start until it's enabled. It's a little bit more complex than "this before that", though these are the most common use cases. 

Edited by RenzoCoppola

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!