Hello all,
Ok, I did a deep search right now and I have the same problem.
I have a GameObject to instantiate and I’m throwing to a variable, like everyone else (in C#). With Javascript, I could make it work, but in C#, I just can’t (maybe something stupid I’m doing).
BUT I find other posts with same problems and nobody answered/solved the problem.
http://forum.unity3d.com/viewtopic.php?t=60225&highlight=instantiate
http://forum.unity3d.com/viewtopic.php?t=52931&highlight=cannot+cast+source+type+destination+type
http://forum.unity3d.com/viewtopic.php?t=48576&highlight=cannot+cast+source+type+destination+type
What I’m trying do to (example):
public GameObject enemy;
void Update(){
if (someVar == 1){
GameObject newEnemy = GameObject.Instantiate(enemy, transform.position, transform.rotation) as GameObject;
newEnemy.SendMessage("SetField", 1);
}
}
This returns me this error when the GameObject got instantiated (in line of the SendMessage):
NullReferenceException: Object reference not set to an instance of an object
I tried this line too:
GameObject newEnemy = (GameObject)GameObject.Instantiate(enemy, transform.position, transform.rotation) as GameObject;
And this too:
GameObject newEnemy = (GameObject)Instantiate(enemy, transform.position, transform.rotation);
All of this compile with no errors, but when I play this thing and the Object got instantiated, it returns that error above or the
InvalidCastException: Cannot cast from source type to destination type
Anybody has a workaround for this?
I searched the forum and looks like lots of people had problems with that, but I did not see an answer. I tried even using that typeof but no sucess.
I really think its something stupid I’m doing… lol