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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Update only called once in Monobehaviour

Discussion in 'Scripting' started by mightyboat, May 3, 2020.

Thread Status:
Not open for further replies.
  1. mightyboat

    mightyboat

    Joined:
    Apr 10, 2016
    Posts:
    14
    I have procedural generated level that spawns prefabs with monobehaviours that have some timed actions. These prefabs work fine when dropped into the scene directly. However when instantiated by the level generator, the update method is only called once which means the timed actions do not update.

    I have checked the scene while the game is running to make sure the game objects and components are all activated, and they are, so it should work.

    Any ideas what can cause the Update method to stop being called?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    How did you test that the Update method is only run once? Are there any exceptions? Unity runs the Update functions of alive and active gameobjects once per frame. If they are not run, then the object would have to be destroyed, set inactive, or the execution of the method would have to be prevented by an occuring error.
     
    Bunny83 likes this.
  3. mightyboat

    mightyboat

    Joined:
    Apr 10, 2016
    Posts:
    14
    I added a print statement at the top of the Update method of my monobehaviour before any other code. I see the statement appear only once per prefab instance. Also, there's a public variable that holds the timer value which, when it's working can be seen to decrease in value, but in this case it stays at the same value.

    Also I checked the instantiated prefabs in the scene while the game is running, and they're all active.

    Maybe there's a silent error?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Are you sure it's actually only logging once? Maybe Unity's console window is automatically collapsing duplicated messages. You can turn this feature off by toggling "Collapse" on the console:
    upload_2020-5-3_20-4-37.png

    Another possibility: Are you manually calling the Update method somewhere? It's possible you have the method signature wrong but it's getting called once manually. Sharing your script would be helpful.
     
    Bunny83 and Kurt-Dekker like this.
Thread Status:
Not open for further replies.