Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party PhotonView RPC not working on OnEnable()

Discussion in 'Multiplayer' started by mattsartori, Jan 13, 2022.

  1. mattsartori

    mattsartori

    Joined:
    Sep 30, 2019
    Posts:
    2
    Hi Guys.

    On my game, i'm spawning a monster with PhotonNetwork.Instantiate() eventually, and on the first time it works perfectly.

    On the other times, the Start() method isn't called because the gameObject is "cached" on Photon.

    But if i try to call the RPC that i called previously on Start(), but on OnEnable(), it says that the photonView doesn't exists.

    Any hint of how can i work with this?

    I think the photonView isn't ready yet on the OnEnable(), but the Start() is not triggered...
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Good observation. Yes, the objects should be cached and Start() is only called once.
    If you use a MonoBehaviourPun, this should actually find the PhotonView on demand, so it should not be null in OnEnable(). In many other cases, execution order will matter. The components on a GameObject should initialize in the order you place them on the object (so move components up or down as needed).

    That aside: Why would you call an RPC in OnEnable, if you instantiate the object anyways?! I mean: The instantiation is networked. This is an event you could hook in to, to execute code. Why RPC anything on top of that??
     
    Last edited: Jan 14, 2022
    mattsartori likes this.
  3. mattsartori

    mattsartori

    Joined:
    Sep 30, 2019
    Posts:
    2
    Thanks!

    About the RPC in OnEnable, I didn't think to do that, it really makes more sense!