- Home /
Question by
thenash654 · Jan 23, 2018 at 10:43 PM ·
unity 5multiplayer
Accessing NetworkServer.Spawn Object From Client
Hello, everyone!
I'm trying to develop a small, multiplayer game just to understand how UNET works. I want to spawn an object (trail for the player) and change it's scale. So far, only the server is able to modify the scale for the object, which gets synced to the client. If I try to do so from the client, it gives me a null reference error and i cannot modify the scale to begin with.
I searched through the forums and cannot find anything useful or comprehensive. Can someone please tell me how I can access an object spawned by the NetworkServer? Thanks.
Here is my code:
using UnityEngine;
using UnityEngine.Networking;
public class PlayerController2 : NetworkBehaviour
{
private int direction = -5;
public GameObject trail;
private GameObject currentTrail;
public GameObject glow;
private float speed = 0.3f;
private float size = 1.25f;
private float time = 0;
private Vector3 pos;
public float trailScale;
void Update()
{
if (!isLocalPlayer)
return;
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0 && direction != 1) //left
{
this.direction = 0;
transform.eulerAngles = new Vector3(0, 180, 0);
Vector3 pos = new Vector3(transform.position.x + size / 2 + 0.4f, transform.position.y, transform.position.z);
CmdCreateTrail(trail, pos, transform.rotation);
trailScale = 0;
}
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0 && direction != 0) //right
{
this.direction = 1;
transform.eulerAngles = new Vector3(0, 0, 0);
Vector3 pos = new Vector3(transform.position.x - size / 2 - 0.4f, transform.position.y, transform.position.z);
CmdCreateTrail(trail, pos, transform.rotation);
trailScale = 0;
}
if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") < 0 && direction != 3) //down
{
this.direction = 2;
transform.eulerAngles = new Vector3(0, 90, 0);
Vector3 pos = new Vector3(transform.position.x, transform.position.y, transform.position.z + size / 2 + 0.4f);
CmdCreateTrail(trail, pos, transform.rotation);
trailScale = 0;
}
if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") > 0 && direction != 2) //up
{
this.direction = 3;
transform.eulerAngles = new Vector3(0, -90, 0);
Vector3 pos = new Vector3(transform.position.x, transform.position.y, transform.position.z - size / 2 - 0.4f);
CmdCreateTrail(trail, pos, transform.rotation);
trailScale = 0;
}
if (this.direction >= 0 && this.direction <= 3) //move left
{
transform.Translate(speed, 0, 0);
trailScale += speed / 2;
this.currentTrail.SendMessage("CmdSetScale", trailScale);
}
}
void CreateGlow()
{
Instantiate(glow, transform.position, transform.rotation);
}
[Command]
void CmdCreateTrail(GameObject obj, Vector3 posi, Quaternion rot)
{
this.currentTrail = Instantiate(trail, transform.position, rot);
NetworkServer.SpawnWithClientAuthority(currentTrail, connectionToClient);
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "Trail")
print("COLLISION");
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612151052im_/https://answers.unity.com/themes/thub/images/avi.jpg)