I have the following lines of code in two different C# scripts. In one of them it works flawlessly in the other one I get a “NullReferenceException” error:
private GameObject goh1;
private GameObject goh2;
private GameObject goh3;
private GameObject goh4;
private GameObject goh5;
private GameObject goh6;
private PlayerCharacter pcClass1;
private PlayerCharacter pcClass2;
private PlayerCharacter pcClass3;
private PlayerCharacter pcClass4;
private PlayerCharacter pcClass5;
private PlayerCharacter pcClass6;
// Use this for initialization
void Start () {
goh1 = GameObject.Find("PC1");
goh2 = GameObject.Find("PC2");
goh3 = GameObject.Find("PC3");
goh4 = GameObject.Find("PC4");
goh5 = GameObject.Find("PC5");
goh6 = GameObject.Find("PC6");
pcClass1 = goh1.GetComponent();
pcClass2 = goh2.GetComponent();
pcClass3 = goh3.GetComponent();
pcClass4 = goh4.GetComponent();
pcClass5 = goh5.GetComponent();
pcClass6 = goh6.GetComponent();
//some stuff that is different between the two files
}
Error message:
NullReferenceException
UnityEngine.GameObject.GetComponent[PlayerCharacter] () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:18)
Targetting.Start () (at Assets/Scripts/CharacterMechanic/Targetting.cs:43)
Any idea on how to get rid of that error? How is that even possible?