Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnEnable/OnDisable #330

Closed
dragonslaya84 opened this issue Aug 3, 2020 · 5 comments
Closed

OnEnable/OnDisable #330

dragonslaya84 opened this issue Aug 3, 2020 · 5 comments

Comments

@dragonslaya84
Copy link
Member

Describe the bug
Since going to events. Most events usually sit in OnEnable / OnDisable. Thats where i put most of the events to tap into for OnStartServer or OnStartClient etc. Some of the objects like my bullets and smooth sync seem to not like it. It randomly at times fires the OnEnable before Server is set inside of the NetworkBehaviour.

Repro project
My Project is huge. I will try to setup something with examples.

To Reproduce
Place a Server.Active check inside of OnEnable of a NetworkBehaviour script of an object being spawned in. It will give errors.

Expected behavior
Expected behaviour would be to have the setting of variables way before OnEnable and OnDisable ever fires off like in awake or Start. Im not sure how or where the Server variable gets set inside of NetworkIdentity.

Screenshots
@uweenukr Was helping me and saw most of my screenshots if need be I can take more.

Desktop (please complete the following information):

  • Windows 10 x64
  • standalone unity editor
  • 2019.4.0f1
  • MirrorNG upm
@uweeby
Copy link
Member

uweeby commented Aug 3, 2020

Also of note this was found while converting a Mirror based project over to MirrorNG. Aside from small renames and refactors no changes to NG core were made. This issue seems to be based on features/timing of Mirror and differences in NG.

I am not great at object/sync stuff but an idea that came to mind would be adding an OnEnable/OnDisable style event to NetworkIdentity. I have not yet tested if the StartClient and StartServer events satisfy the timing of this issue.

@uweeby
Copy link
Member

uweeby commented Aug 4, 2020

I was able to confirm the issue via tests and confirmed a potential solution/workaround

public class SampleBehavior : NetworkBehaviour
{
    public void Start()
    {
        NetIdentity.OnStartServer.AddListener(Enable);
    }

    public void Enable()
    {
        //All Tests Pass
        if(Server.Active) { }
    }

    public void OnEnable()
    {
        //All Tests Fail
        if (Server.Active) { }
    }

@dragonslaya84
Copy link
Member Author

dragonslaya84 commented Aug 5, 2020 via email

@paulpach
Copy link
Contributor

paulpach commented Oct 4, 2020

This is by design.

The object may be instantiated in the server and never spawned. There may also be multiple servers, so until an object is spawned, there is no way to know which Server or Client it will be used in.

If you need to initialize something when the object is networked, then subscribe to NetIdentity.OnStartServer or NetIdentity.OnStartClient and initialize your object there instead.

@uweeby
Copy link
Member

uweeby commented Jan 19, 2021

Can we close this? Is there going to be a revisit? Does it need to be written in documentation?

@uweeby uweeby closed this as completed Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants