- Home /
MLAPI error with two instances of networked object
I am having an error that I am having a lot of trouble understanding, it happens when the client player instantiates then spawns a prefab, for some reason the prefab can no longer access a parent camera but only when there are two or more players, otherwise it works without any issues. Here is the complete error i@3e3aef6aa0/Runtime/Spawning/NetworkSpawnManager.cs:292) MLAPI.Messaging.InternalMessageHandler:HandleAddObject(UInt64, Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/InternalMessageHandler.cs:270) MLAPI.NetworkManager:HandleIncomingData(UInt64, NetworkChannel, ArraySegment
1, Single, Boolean) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:982) MLAPI.NetworkManager:HandleRawTransportPoll(NetworkEvent, UInt64, NetworkChannel, ArraySegment`1, Single) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:875) MLAPI.NetworkManager:OnNetworkEarlyUpdate() (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:683) MLAPI.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:641) MLAPI.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage)"
it is happening since i coded this to allow theprefab to be instantiated over the network
public class EquipWeapon : NetworkBehaviour
{
[SerializeField]GameObject weapon;
[MLAPI.Messaging.ServerRpc]
void EquipServerRpc(ulong playerId)
{
GameObject instantiatedWeapon = Instantiate(weapon, transform.Find("PlayerCamera").transform.Find("GunSocket"));
instantiatedWeapon.GetComponent<NetworkObject>().SpawnWithOwnership(playerId);
ulong weaponNetId = instantiatedWeapon.GetComponent<NetworkObject>().NetworkObjectId;
EquipClientRpc(weaponNetId);
}
[MLAPI.Messaging.ClientRpc]
void EquipClientRpc(ulong netId)
{
NetworkObject spawnedWeapon = NetworkSpawnManager.SpawnedObjects[netId];
GameObject equippedItem = spawnedWeapon.gameObject;
equippedItem.transform.localRotation = Quaternion.identity;
equippedItem.transform.localPosition = Vector3.zero;
}
private void Update()
{
if (IsLocalPlayer)
{
if (Input.GetKeyDown(KeyCode.B))
{
EquipServerRpc(NetworkManager.Singleton.LocalClientId);
}
}
}
}
However as I said this code works just fine in singleplayer, but with another player, as soon as the weapon spawns i get the error above and cannot spawn anymore of this weapon even for the other player. the error comes from this script which contains all the weapon stats:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
public class BaseWeaponScript : NetworkBehaviour
{
public float damage;
public float magBullets;
public float currentMagBullets;
public float totalBullets;
public float fireRate;
[NonSerialized]public GameObject player;
[NonSerialized]public Camera playerCamera;
[NonSerialized]public Animator animator;
void Awake()
{
player = transform.parent.parent.parent.parent.gameObject;
playerCamera = transform.parent.parent.parent.gameObject.GetComponent<Camera>();
animator = GetComponent<Animator>();
}
}
Sorry for the long question but i have been on this issue for a while now and just have no clue on what the actual issue is, thank you to anyone who can help.
this is the beginning of the error
BaseWeaponScript.Awake () (at Assets/Scripts/Weapons/BaseWeaponScript.cs:22) UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion) which seems like a normal error until developped where i see the rest of the error as seen aboveNullReferenceException: Object reference not set to an instance of an object