Getting Error "Disconnecting c... to instantiate is null."

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

I’d say that’d be the important part. When you’re instantiating an object, it cannot be null.

but why ? I cant figure out why it becomes null, when I am giving a valid gameobject as parameter in CmdSpawnUnit() call

Hey there, did you add the prefab to the list of spawnable prefabs on the network manager?

yep I have done that
also, I am able to spawn the unit if I spawn it with direct reference to global var. not one which I take as input in function

Later in the course we will change how this is set up anyway. We will use unit Ids and unit queues instead of passing gameObjects, so the error shouldn’t persist after that. I guess keep working ahead and let me know if you have other problems down the road.

OKAY :slight_smile: WILL CONTINUE THEN :stuck_out_tongue:

I think you cannot pass GameObject references over the network, as Mirror recognizes it as a security risk. You need to use actual values and not object references. I guess this has changed.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms