Unexplainable NullReferenceException?
So, making a tetris game, and trying to instantiate from a resources folder, here is the stack trace:
namespace GameManager
{
public class GameManager:MonoBehaviour
{
Instantiator instance;
void Start()
{
instance.Spawn();
}
public void CheckInput()
{
var posit = transform.position.y;
if (posit != -10)
{
if (Input.GetKeyUp(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);
}
else if (Input.GetKeyUp(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);
}
else if (Input.GetKeyUp(KeyCode.UpArrow))
{
transform.Rotate(0, 0, 90);
}
else if (Input.GetKeyUp(KeyCode.DownArrow))
{
transform.position += new Vector3(0, -1, 0);
}
}
else
{
return;
}
}
void Update()
{
CheckInput();
}
}
}
Then,
public interface Iinstantiatable
{
void SearchAndCall();
}
public class Instantiator:MonoBehaviour
{
Fire.Fire fireElement;
Ice.Ice iceElement;
Lightning.Lightning lightningElement;
Terra.Terra terraElement;
Water.Water waterElement;
Wind.Wind windElement;
public void Spawn()
{
System.Random RandomElement = new System.Random();
int Case = RandomElement.Next(6);
switch(Case)
{
case 1:
fireElement.SearchAndCall(); break;
case 2:
iceElement.SearchAndCall(); break;
case 3:
lightningElement.SearchAndCall(); break;
case 4:
terraElement.SearchAndCall(); break;
case 5:
waterElement.SearchAndCall(); break;
case 6:
windElement.SearchAndCall(); break;
}
}
void Start()
{
}
void Update()
{
}
}
Then, for example:
namespace Fire
{
public class Fire:MonoBehaviour, Iinstantiatable
{
string TetroName;
public void SearchAndCall()
{
GameObject Tetro = (GameObject)Instantiate(Resources.Load(ChooseFireShape(), typeof(GameObject)), new Vector2(0, 12f), Quaternion.identity);
}
public string ChooseFireShape()
{
System.Random RandomFire = new System.Random();
int Case = RandomFire.Next(9);
switch(Case)
{
case 1:
TetroName = "FireTypes/I_Fire"; break;
case 2:
TetroName = "FireTypes/+_Fire"; break;
case 3:
TetroName = "FireTypes/Dot_Fire"; break;
case 4:
TetroName = "FireTypes/l_Fire"; break;
case 5:
TetroName = "FireTypes/LargeL_Fire"; break;
case 6:
TetroName = "FireTypes/Rect_Fire"; break;
case 7:
TetroName = "FireTypes/Sqr_Fire"; break;
case 8:
TetroName = "FireTypes/T_Fire"; break;
case 9:
TetroName = "FireTypes/U_Fire"; break;
}
return TetroName;
}
void Start()
{
}
void Update()
{
}
}
}
The error occurs at GameManager.instance.Spawn()
.Following the trace back, I still don't see why there is a NullReferenceException. Any ideas why? Or maybe a bug in my code?
The folder "FireTypes" is in the Resources folder which is in the Assets folder, just to make that clear.
the instance variable of type Instantiator in the Game$$anonymous$$anager is private. This means it could not have been assigned to via inspector. Where do you create the actual object for this variable?
The Instantiator variable is private because it comes from the public class Instantiator:$$anonymous$$onoBehaviour
. It need not a creation or to be assigned via the inspector.
that's the reason for the NullRef. just creating a variable for a reference type defaults to null.you need to assign a real object somehow. You could use the singleton pattern, or assign it the object in the inspector, or search for it with GameObject.Find,or... anyway, the variable needs a proper value
Answer by SoraMahiro · Dec 10, 2016 at 07:07 PM
I actually found the problem. The problem wasn't about the variable not having a value, it was a problem of the variable not being able to take a value, what I mean by that is that I was calling a variable in this case instance
without allowing a way to get the reference. My fix was using FindObjectOfType<Instantiator>
. This allowed me to find the script on my Game Manager object. It also was used as FindObjectOfType<Fire>
and as the other elements to be able to find those scripts, after that I had no problem Instantiating, but as game development goes, I came across another problem.