Search Unity

Question Spawn NetworkObject and change sprite at runtime

Discussion in 'Multiplayer' started by davidegalifi, Jan 13, 2024.

  1. davidegalifi

    davidegalifi

    Joined:
    May 24, 2023
    Posts:
    6
    I'm a super noob with Game Development and with Unity and I've been playing with networking but I think there's something I'm missing
    The idea is really simple. I'm instantiating a GameObject at runtime and its color depends on some variables loaded at runtime but for simplicity let's supposed I want to set it as red at runtime.

    My GameManager Start() method looks like this

    Code (CSharp):
    1.  var prefab = Instantiate(Prefab);
    2.  
    3. var prefabBehaviour = prefab.GetComponent<MyBehaviour>();
    4. prefabBehaviour.GetComponent<SpriteRenderer>().color = Color.red;
    5.  
    6. prefabBehaviour.NetworkObject.Spawn();
    7.  
    8.  
    The result is that the object is instantiated as red on the server but on client it's spawned with the default color.

    How would I achieve something like this?
     
    Last edited: Jan 13, 2024
  2. davidegalifi

    davidegalifi

    Joined:
    May 24, 2023
    Posts:
    6
    I’m not sure about this Photon library but I’m fairly sure you can’t instantiate a game object on the client
     
  3. darceyo

    darceyo

    Joined:
    Jan 10, 2024
    Posts:
    29
    Do you know how multiplayer works?
     
  4. davidegalifi

    davidegalifi

    Joined:
    May 24, 2023
    Posts:
    6
    No I don’t. I’ll give it a try