Mirror icon indicating copy to clipboard operation
Mirror copied to clipboard

Awake not being called on NetworkBehaviour Component

Open jekuper opened this issue 3 years ago • 2 comments

Describe the bug Awake is not called on objects with NetworkBehaviour unless server/host/client is started.

Reproduce In "Basic" example, open scene. Create an empty object in scene. Create a test.cs file with the following code:

using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : NetworkBehaviour
{
    private void Awake () {
        Debug.Log ("awake called");
    }
}

Attach component to created gameobject Start scene in editor. Look at the console output, this should be there: Screenshot 2022-09-10 224535 Next, Build this scene with that settings: image Then, run the resulting build. Then close the game application. Next navigate to: C:\Users\YourUser\AppData\LocalLow\YourCompany\YourProjectName\ on your windows computer Open file Player.log Understand that there is no "awake called" message. image

Expected behavior Expected "awake called" message in Player.log file

Desktop:

  • OS: Windows
  • Build target: standalone
  • Unity version: 2021.3.5f1 Personal
  • Mirror branch: idk, I just downloaded from asset store. Mirror version: 66.0.9

Additional context I need Awake in order to create singleton. I would appreciate any other solution, however this is very odd that Editor and Build versions behaves differently.

jekuper avatar Sep 10 '22 17:09 jekuper

I faced this issue as well. Very annoying when Editor and Build behavior is different. Hope it gets fixed soon.

ShirChoi avatar Sep 11 '22 06:09 ShirChoi

My understanding is that Mirror disables all GameObjects inside a Unity scene with a NetworkBehaviour on them. It then re-enables them on each client once they have been spawned. This is documented here.

In your case, unfortunately, since the no client / host is started, the GameObject never gets enabled.

Ikalou avatar Sep 12 '22 14:09 Ikalou

thanks for the detailed report :)

as @Ikalou mentioned, scene objects are only loaded once they are spawned. until then, they are meant to behave similar to prefabs (=unloaded).

this is because we don't want their awake/update/etc. to run before spawned. for example, a scene object monster on the client should not move around before it was even spawned over the network.

this isn't strictly necessary, but we figured it makes most sense this way. do you think it should be changed?

miwarnec avatar Sep 24 '22 15:09 miwarnec

do you think it should be changed?

Well, I don't think it should be changed and now I understand why this is needed, however, in my opinion it's not good when Editor and Build behaviour is different.

jekuper avatar Sep 24 '22 17:09 jekuper