Awake being called twice

Unity was working as it should. I had two hidden scripts :/.

1 Like

How are you determining that Awake is being called twice?

Agreed w/ A.Killingbeck, how do you know it’s being called twice?

Are you sure you don’t have 2 of the same script attached to gameobjects in your scene?

2 Likes

A debug statement. New scene + empty game object + single script with (void Awake())

There’s probably just something weird I’m doing. But putting a flag to make sure awake doesn’t get called more than twice fixes it all. And I know there’s not some strange second script somewhere cause the flag fixed it. If there were two separate scripts then I would have seen the message logged twice.

That’s just a bandaid… you should figure out why Awake is being called more than once. Because it shouldn’t be.

Are you calling ‘SendMessage(“Awake”)’ anywhere?

What does your code look like?

Nevermind. I wrote a search script which searched for any given script in the scene. Looks like I accidentally dropped the script on something else buried in the hierarchy so there were two instances (facepalm).

Hi.
Recently I also had such an issue.
Awake() was called twice when I exited from play mode.
After some research through SmartGit change-logs found out that the error was after I had added
[ExecuteInEditMode] for the Script.

I hope this will help others also.

3 Likes

My problem was the same, did you find a workarround to fix it without removing the [ExecuteInEditMode] ?

1 Like

In my case it was ok to remove [ExecuteInEditMode].
But you can run the body of Awake() method only when application is in “playing” state.

    private void Awake()
    {
        if (Application.isPlaying)
        {
            // Do Something
        }
    }
1 Like

This comment just helped me 13 years later. I had two…

Unfortunately necroposting didn’t help anyone else. Please don’t do this, instead use the “Like” button to show your appreciation.

Thanks.

Thread Closed.

1 Like

I saw the Awake can be called 2 times if it’s a class in a scene loaded with SceneManager.LoadSceneAsync