0

EDIT: In addition to thr answer below, these links helped resolve the issue...

https://forum.unity.com/threads/ui-buttons-to-spawn-units-buildings.356813/ https://forum.unity.com/threads/network-spawned-objects-not-showing-up-at-other-clients.501724/

I am instantiating and parenting an object, a shot from a spaceship, which should display on all player screens. This works only when the host fires a shot. When a client fires a shot, only that client can see the shot.

For network identity, the ship (parent) is set to local player authority. For the shot, neither local or server are selected.

This is the code that creates the shot...

public class fire : NetworkBehaviour{

public GameObject shockwave;
public GameObject ship;
private GameObject eb;
private GameObject go;

private int energy = 4;
private float targetTime = 10.0f;

public override void OnStartLocalPlayer()
{
    base.OnStartLocalPlayer();
    gameObject.name = "Local";
}

public void createshockwave()
{
    ship = GameObject.Find("Local");
    Debug.Log(ship);
    if (!ship.GetComponent<NetworkIdentity>().isLocalPlayer)
    {
        return;
    }

    go = Instantiate(shockwave, ship.transform.position, ship.transform.rotation);
    go.transform.parent = ship.transform;
    NetworkServer.Spawn(go);
}
}

Any clues as to what might cause the object to be displayed on other screens when shot by the host, but not client?

tintyethan
  • 1,772
  • 3
  • 20
  • 44
  • have u checked whether the client received the command to instantiate the gameobject? – Lincoln Cheng Jun 03 '18 at 04:01
  • I guess I don't know how to do that. When I debug in Visual Studio, I get a break on the createshockwave method, and it is created on the client, but is not created on other clients or the host. The shot is only visible on all clients when its called from the host. The code runs the same regardless. – tintyethan Jun 03 '18 at 04:28

1 Answers1

1

It's a little bit tricky and I suppose you are working with UNet and not Photon.

I cannot say for sure if I have a proper solution, but I managed to make it work using the ClientRPC and Command actions available in UNET.

Using [Command] action infront of a method lets a client make a call to the server and using [ClientRPC] action will call on the server and run on the clients.

So I made a possibly unsafe, but doable approach where you can simply call a [Command] method and within this you call a [ClientRPC] method. Which will make what you desire possible.

You can read up on it here https://docs.unity3d.com/Manual/UNetActions.html

Skdy
  • 280
  • 1
  • 5
  • 16
  • Yep. I finally started finding the solution after I posted this. I've added some links above that were concise and helped me. – tintyethan Jun 03 '18 at 18:05