Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why does Update() apply the image, but Start() doesn't?

Discussion in 'Editor & General Support' started by IIBearWithII, May 16, 2023.

  1. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    I'm loading an image from the internet in an Awake() method. I applied the image in Start() and it doesn't work. I've been experimenting to fix it and I tried applying the image in Update() and it worked, but I'm not sure how. Both Start() and Update() happen after Awake(), so why should that have made a difference?
     
  2. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Hmmm, apparently a coroutine that is started in Awake() doesn't hold up the program from moving on to Start(). My understanding was that Start() wouldn't happen until ALL Awake() methods were done, and that meant, to me, that a coroutine started in an Awake() method had to be finished too. But apparently that's not the case...
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Coroutines don't hold up the program when they yield, and that is the entire point of using a coroutine.
     
    IIBearWithII likes this.
  4. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    I understood that, but then kind of forgot about that when I was focusing on Awake() happening before Start(). But not all Awake() methods will necessarily be done before some Start() methods begin, once coroutine get involved sometimes. Is that really how things work?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Generally you fill out the data after you get it back in the coroutine.
     
    IIBearWithII likes this.
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Well... yes. If you allow something in Awake to execute over multiple frames, it will then bleed into when and after Start has been called, and Update begins running. Unity isn't going to hold up the entire execution in case a coroutine happens to begin in Awake.
     
    IIBearWithII likes this.
  7. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    702
    I have had an issue like this.

    The UI get image rects quite late in the process of a first frame. So if you are using their dimensions to determine your image size, they oft come in at size 0,0.

    I integrate 3d models in my ui, so i need those rect values to drive the scales of the models in the ui window. Same with windows that need the dimensions of text inside to size dynamically.

    Solution is to create a yield return null coroutine, basically, wait for the second frame of scene load, and the rect values will be available.

    I believe i read this was actually a bug, and was fixed in a 2022 release, but i could be misyaken. Either way, i simply can no longer trust rect values to be available in first frame or instantiation of a new ui rect.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Not a bug, UI stuff it just updated at a point in the player loop after Update, generally in Late Update (or later). You can see all the various points of each stage of the player loop here in the C# ref.

    Most UI stuff happens in post-late update stage. So what you're doing - waiting a frame - is pretty common. This is where plugins like UniTask make things easier by letting you use C# async code.
     
  9. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    702
    Thanks. I guess thinking lateupdate was the very last before frame draw was my bad? Anyways, thanks for links, but a little too complicated for me at this juncture. Ill stick to next frame co routine for now, it works pretty well, and optimize later if necessary.