- Home /
Failing to reference an instantiated object
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.
What is 'whatToSpawn'? How are you defining that variable? What line of code does the error occur on?
It seems to me like there is nothing wrong with this code. I suspect that you are not saving enemy anywhere else after instantiating it, yet trying to operate as if you had. Enemy is local to that function. If you have 'enemy' anywhere else in the script, this function will not save this reference there, it will save it in the local one. If that is the problem, then this should fix it:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))
{
enemy = Instantiate(whatToSpawn, this.transform.position, this.transform.rotation) as GameObject;
enemy.GetComponent<Enemy>().changeStats(health, damage);
}
edit: also, I believe you put the 'as' statement next to the 'whatToSpawn':
enemy = Instantiate(whatToSpawn as GameObject, this.transform.position, this.transform.rotation);
whatToSpawn is declared outside the method like this:
public Transform whatToSpawn;
Apart from this, the script has virtually no other code. Neither of your suggested code snippets solved the problem, unfortunately. The prefab does get instantiated, however, once I try to access the Enemy component, it says that it is missing a reference to the object. It's as if I would instantiate it as enemy GameObject, and then in the next line it no longer remembers this just created object. $$anonymous$$aybe you're right that I am not storing/saving it properly. If that's the case, I don't know how to do that.
Answer by ninja_gear · May 30, 2016 at 02:51 PM
make sure whatToSpawn is a GameObject, or your code will choke when attempting to cast with as and null will be returned
This solved the problem! Thanks a bunch!
That's strange, though. I always see people expose their game object variables as Transforms.
Thanks again!
Always save what you are going to use:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))
{
Enemy enemy = Instantiate(whatToSpawn as GameObject, this.transform.position, this.transform.rotation).GetComponent<Enemy>() ;
enemy.changeStats(health, damage);
}
Answer by Arsineh · May 30, 2016 at 02:45 PM
did you initialize your public variable in Inspector? probably when you create an object in script and it is public, you will see the variable in inspector and you need to give it a parameter to initialize.
Hope this can solve the problem
Yeah, the problem was that I stored the prefab as a Transform, and not as a GameObject. I'm still not sure why, though, Transform usually gets the job done, and I thought it would be castable as a GameObject anyway. It wasn't.
Thank you for giving it a shot!
Answer by vittu1994 · May 30, 2016 at 01:58 PM
Try:
GameObject enemy = (GameObject)Instantiate(whatToSpawn, this.transform.position, this.transform.rotation);
Now it says
Cannot cast from source type to destination type.
Is the Enemy script attached to the enemy object you are trying to instantiate?
Yes, it is. It was even available as an auto-complete suggestion, along with the other scripts the prefab has.