Hello I was just following Nathan on the unity multiplayer course and at one point when I am trying to spawn a unit I was getting the following error when I instantiate then the unit in code
Disconnecting connId=0 to prevent exploits from an Exception in MessageHandler: ArgumentException The Object you want to instantiate is null.
the following script is giving me the error
using System.Collections;
using System.Collections.Generic;
using Mirror;
using UnityEngine;
using UnityEngine.EventSystems;
public class Spawner: NetworkBehaviour, IPointerClickHandler
{
[SerializeField] Transform spawnPoint;
[SerializeField] GameObject spawnUnit;
#region Server
[Command]
private void CmdSpawnUnit(GameObject unit){
GameObject spawnedUnit = Instantiate(unit,spawnPoint.position, spawnPoint.rotation);
NetworkServer.Spawn(spawnedUnit, connectionToClient);
}
#endregion
#region Client
public void OnPointerClick(PointerEventData eventData)
{
if(eventData.button != PointerEventData.InputButton.Left){ return; }
if(!hasAuthority){ return; }
CmdSpawnUnit(spawnUnit);
}
#endregion
}
I tried some debug statements also to see why the reference is getting null
so I tried
Debug.log(spawnUnit) just before calling CmdSpawnUnit func and Debug.log(unit) inside CmdSpawnUnit func.
and as output I got
Unit for 1st log statement
and
null for the 2nd one
don’t know why it doesn’t get passed properly inside func.
as a temporary solution, I just modified the code for now
using System.Collections;
using System.Collections.Generic;
using Mirror;
using UnityEngine;
using UnityEngine.EventSystems;
public class Spawner : NetworkBehaviour, IPointerClickHandler
{
[SerializeField] Transform spawnPoint;
[SerializeField] GameObject spawnUnit;
#region Server
// [Command]
// private void CmdSpawnUnit(GameObject unit){
// Debug.Log(spawnUnit);
// GameObject spawnedUnit = Instantiate(unit,spawnPoint.position, spawnPoint.rotation);
// NetworkServer.Spawn(spawnedUnit, connectionToClient);
// }
[Command]
private void CmdSpawnUnit(){
GameObject spawnedUnit = Instantiate(spawnUnit,spawnPoint.position, spawnPoint.rotation);
NetworkServer.Spawn(spawnedUnit, connectionToClient);
}
#endregion
#region Client
public void OnPointerClick(PointerEventData eventData)
{
if(eventData.button != PointerEventData.InputButton.Left){ return; }
if(!hasAuthority){ return; }
// CmdSpawnUnit(spawnUnit);
CmdSpawnUnit();
}
#endregion
}
but if it will create a problem later when we add more types of spawn units
so plz help me