- Home /
NullReferenceException and m_InstanceID == 0 on LoadLevel (C#)
So I have a script that spawns in my player as soon as the level loads. It works perfectly fine when loading the scene straight-up.
However, when I load that scene from another scene using the LoadLevel function, I get these errors.
m_InstanceID == 0 UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) GameMaster:Start() (at Assets/Scripts/PlayerScripts/Character Classes/GameMaster.cs:12)
NullReferenceException UnityEngine.GameObject.GetComponent[PlayerCharacter] () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineGameObject.cs:30) GameMaster.Start () (at Assets/Scripts/PlayerScripts/Character Classes/GameMaster.cs:13)
Both of them link me to the same lines of code:
public GameObject playerCharacter;
private GameObject _pc;
private PlayerCharacter _pcScript;
void Start () {
_pc = Instantiate (playerCharacter, Vector3.zero, Quaternion.identity) as GameObject;
_pcScript = _pc.GetComponent<PlayerCharacter>();
_pc.name = "pc";
}
I'm at my wit's end here. I've looked up other ways to fix the m_instanceID problem and none of them have worked.
Answer by EmpI · Feb 05, 2016 at 06:14 AM
And I fixed my own problem within minutes of asking the question.
For anyone who stumbles onto this with a similar problem, make CERTAIN your instantiated object when called from the previous scene DOES NOT have a "DontDestroyOnLoad" script.