Question by
SwiftKraft · Sep 07, 2021 at 05:40 PM ·
c#nullreferenceexception
Weird Error With References
Here is the code:
foreach (GameObject go in Dependencies)
{
BossDependency dep = Instantiate(go, transform.position, transform.rotation).GetComponent<BossDependency>();
existingDependencies.Add(dep);
dep.BossObject = this;
}
The main issue is that it just throws an Object reference not set to an instance of an object
error and stops the foreach
loop. I tried using Debug.Log(dep)
but it shows that it exists.
Comment
Best Answer
Answer by SwiftKraft · Sep 07, 2021 at 05:52 PM
Nevermind, I figured it out, its just a simple mistake of me not doing this:
Original:
List<BossDependency> existingDependencies;
After:
List<BossDependency> existingDependencies = new List<BossDependencies>();
I was looking in the completely opposite direction, I hope unity could make the error message clearer by saying what value was not referenced.