- Home /
How could I diferentiate between clones?
Hi everyone, ive got stuck on a piece for the game im working on
first of all the problomatic code
part 1
function OnGUI ()
{
if(isincombat)
{
GUI.Label (Rect (700, 10, 500, 300), "" + enemymonster.name);
GUI.Label (Rect (700, 30, 500, 300), "" + enemymonster.baseHP + "/" + enemymonster.currHP);
GUI.DrawTexture (Rect(900, 130, 128, 128), enemymonster.image);
if(spawned == false)
{
Instantiate(enemymonster.gamemodel,enemymonsterspawn.transform.position,enemymonsterspawn.transform.rotation);
spawned = true;
}
GUI.Label (Rect (10, 10, 500, 300), "" + equippedmonster.name);
GUI.Label (Rect (10, 30, 500, 300), "" + equippedmonster.baseHP + "/" + equippedmonster.currHP);
GUI.DrawTexture (Rect(70, 10, 128, 128), equippedmonster.image);
GUI.Label (Rect (10, 50, 500, 300),"Level " + equippedmonster.level + " XP " + equippedmonster.curXp + " / " + equippedmonster.maxXp);
if(spawned2 == false)
{
Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation);
spawned2 = true;
}
if (!spawned)
{
Destroy(GameObject.Find(enemymonster.name + "(Clone)"));
}
if (!spawned2)
{
Destroy(GameObject.Find(equippedmonster.name + "(Clone)"));
}
if (!displaygui) return;
if(monstersub)
{ for(var k=0; k < 6; k++)
{
if(GUI.Button(Rect(700,150 + (k * 50),100, 30),"" + mymonsters[k].name))
{
Destroy(GameObject.Find(equippedmonster.name + "(Clone)"));
equippedmonster = mymonsters[k];
if(spawned2)
{
Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation);
}
}
}
now the problem im facing is that way im despawning the monsters by finding by name + clone is fine but when the players equipped monster is has the same name as the enemy monster there is no way of telling which one to destroy and it generally destroys the wrong one.
Oh the monsters are an array containing the gamemodels stats etc
hope im making sense and would appreciate any help or maybe a better way of placing and destroying prefabs runtime.
I don't completely understand your setup here, but you can change the name or the tag of a game object. So I suggest you give your enemy monsters and your player monsters different tags or names:
var go = Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation) as GameObject;
go.name = "SomeName";
go.tag = "SomeTag";
Answer by ChristianLinnell · May 01, 2014 at 11:31 PM
I'm not 100% sure I understand your game mechanic, but I'm imagining a Pokemon style game. If that's right, there are two ways to do this.
Local variables
Instead of Instantiating the monsters and then searching for them, have your script store what gets created.
At the top of your script, create two new GameObject variables: equippedMonster and enemyMonster. Then, when you instantiate them, do this:
equippedMonster = Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation);
Then when you go to despawn, you can refer to them directly:
Destroy(equippedMonster);
Unique IDs
If you can't do that for some reason, you could store the unique instance ID of each of your spawned monsters.
spawnedMonster.GetInstanceID();
Easy :-).
thanks for the quick reply christian,
sorry im new at scripting so bare with me.
i tried the first way which while it did make my code tidier it was doig the same thing it was retrieving the name+clone from equipped$$anonymous$$onster but when it dstroys it finds the first name+clone in the heirarchy and destroys that one which is usually the enemy.
on the unique ids i get what they are but no how to use them any tips?
EDIT Never $$anonymous$$d you were right the first time just me rushing ahead o myself and missed some changes in the script
always up for learning if someone waned to explain how id go about it with Get.InstanceID();
Good to hear it's working! Don't forget to accept the answer to help others see the solution :-).
The GetInstanceID() thing is much less helpful, but it's good to be aware that everything in your scene has a unique ID.
Basically if you had to use GameObject.Find() for some reason, you could rename objects you create to match their unique ID, and then store that somewhere, just like how you stored the GameObject you instantiated. Like this:
//Create the object
o = Instantiate(whatever);
//Rename it
o.name = o.GetInstanceID().ToString();
//Store the name
var storedName = o.name;
//Then, later
GameObject.Find(storedName);
It's really not the best way to do it, but it's good to be aware of it. $$anonymous$$ost of the time you can use the method you've just gotten working.
ah i see, i found the object id's one day when i switched to debug and thought ah ha that could come in handy. but could not figure it out thanks for the rpelys christian you have been super helpful. ill accept the answer now or as soon as i figure out how lol
Little tick box next to the upvote button :-)
Your answer
