Make a child object Spawn a game object networking
Hi there, I've just started to code on unity and have started to try to learn about networking. I'm currently trying to make a ship shoot missiles.
Currently, I have a ship with an empty child object to specify the spawn point for the missile. I can currently use the networking tools to spawn ships and move around with two players. The current code makes the missile spawn on the local player but isn't replicated to the others players.
The network manager has the missile in allowed spawned object.
The parent has a network ID and a Network transform.
The instantiated missile has a network ID and a Network transform.
I use a utility function in order to spawn the missile through the main object using a networkBehaviour.
This is the script attached to the empty child object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class WeaponControler : MonoBehaviour {
public GameObject missile;
public float launchSpeed = 10f;
private SpawnUtil spawnUtil;
// Use this for initialization
void Start () {
spawnUtil = GetComponentInParent<SpawnUtil> ();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")){
Fire ();
}
}
void Fire(){
GameObject instance = (GameObject) Instantiate(missile, gameObject.transform.position, gameObject.transform.rotation);
instance.GetComponent<Rigidbody2D>().velocity = instance.transform.up * launchSpeed;
spawnUtil.CmdSpawn(instance);
}
This is the utility script on the parent object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SpawnUtil : NetworkBehaviour {
[Command]
public void CmdSpawn(GameObject entity){
NetworkServer.Spawn (entity);
}
}
When I run two instances of the game, the server gets an error whenever the client shoots. This the error :
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkServer.GetNetworkIdentity (UnityEngine.GameObject go, UnityEngine.Networking.NetworkIdentity& view) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1078)
UnityEngine.Networking.NetworkServer.SpawnObject (UnityEngine.GameObject obj) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1293)
UnityEngine.Networking.NetworkServer.Spawn (UnityEngine.GameObject obj) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1525)
SpawnUtil.CmdSpawn (UnityEngine.GameObject entity) (at Assets/Scripts/SpawnUtil.cs:10)
SpawnUtil.InvokeCmdCmdSpawn (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
UnityEngine.Networking.NetworkIdentity.HandleCommand (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:618)
UnityEngine.Networking.NetworkServer.OnCommandMessage (UnityEngine.Networking.NetworkMessage netMsg) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1281)
UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:469)
UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:425)
UnityEngine.Networking.NetworkConnection.TransportReceive (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:576)
UnityEngine.Networking.NetworkServer.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:738)
UnityEngine.Networking.NetworkServer+ServerSimpleWrapper.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1869)
UnityEngine.Networking.NetworkServerSimple.HandleData (Int32 connectionId, Int32 channelId, Int32 receivedSize, Byte error) (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:384)
UnityEngine.Networking.NetworkServerSimple.Update () (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:247)
UnityEngine.Networking.NetworkServer.InternalUpdate () (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:691)
UnityEngine.Networking.NetworkServer.Update () (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:642)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at /Users/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1091)
I suspect that it is because the child object does not have a network identity (but then again, I cannot add a network Identity to a child object). I don't understand why it would get info on the child when the spawnUtil.CmdSpawn command is called.
Answer by ownezx · Mar 30, 2018 at 09:43 PM
Managed to find the way. I modified the utility script to take argument GameObject toInstanciate GameObject objectPositionReference
Your answer
Follow this Question
Related Questions
How do I rotate a Networked gameobject on spawn? 0 Answers
How to sync SetActive at NetworkServer.Spawn() ? 1 Answer
UNET 5.4 - Spawn an object from client...and move it around from client....? 1 Answer
NetworkServer is not active. Cannot spawn objects without an active server. 0 Answers
NetworkServer.SpawnWithClientAuthority not working? 1 Answer