Ethan Pelton
Ethan Pelton

Reputation: 1796

Object visible on host but not client

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?

Upvotes: 0

Views: 1186

Answers (1)

Skdy
Skdy

Reputation: 290

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

Upvotes: 1

Related Questions