I have a game object that is initiated randomly, then when i collide with an other game object if it is not the same as the randomly instantiated i want the scene to quit.
in the code i print out both names of the object collided with and the rand object and they look the same but it will not work.
Any ideas?
var addclone = certainGameObjectName +“(Clone)”;//
var clonetemp = GameObject.Find(certainGameObjectName +“(Clone)”);
print(clonetemp + " ctn " + addclone );
if (clonetemp !== addclone){
Application.LoadLevel(0);
}
First: you must write “!=”, not “!==”. Anyway, comparing names isn’t a good idea. It would be better to compare tags - tag your prefab, and all clones will have the same tag:
function OnCollisionEnter(col: Collision){
if (col.gameObject.tag != "MyObject") Application.LoadLevel(0);
}
But if you want to use names, name the object right after instantiation, like this:
var clone = Instantiate(objPrefab, ..., ...);
clone.name = "MyObject";
This will ensure all clones have the same name, “MyObject”. You can then safely compare names:
function OnCollisionEnter(col: Collision){
if (col.gameObject.name != "MyObject") Application.LoadLevel(0);
}