I am trying to instantiate an enemy object with some input parameters for the enemy’s health and damage like this:
if (Input.GetKeyDown(KeyCode.T))
{
GameObject enemy = Instantiate(whatToSpawn, this.transform.position, this.transform.rotation) as GameObject;
enemy.GetComponent<Enemy>().changeStats(health, damage);
}
it does spawn the enemy object, but it fails to create a reference to it, and I get "NullReferenceException: Object reference not set to an instance of an object"
error in Unity. What am I doing wrong? I have looked at several threads where this is how people pass parameters to instantiated objects.
I have made sure that the newly instantiated object prefab does in fact have an Enemy component and that it contains the changeStats() method.