- Home /
 
Unique names for Instantiated prefabs?
Hey guys, So I'm trying to create a Combat System. I currently have the Enemy.js script and an EnviromentHandler.js script. The EnviromentHandler instantiates 2 enemies both in different positions:
 var enemySpawn : Rigidbody;
var bossSpawn : Rigidbody;
static var ENEMYBASEHEALTH = 60; 
static var PLAYERBASEHEALTH = 90;
static var SWORDDAMAGE = 25;
static var GUNDAMAGE = 10;
function Start() { var enemyClone : Rigidbody; 
 enemyClone = Instantiate(enemySpawn, Vector3(50,14.3,-19), transform.rotation);
 enemyClone = Instantiate(enemySpawn, Vector3(27,14.3,-19), transform.rotation);
}
Now the problem I have is each of these clones get the Script Enemy.js and when I try to apply damage to the script both of the clones take damage. What i need to do is make them both unique by changing there name...
The simple way to do this would be to do:
enemyClone =
enemyClone1 =
but when I want to do is add a automatic generator to add the number at the end of the name... How is this done?
Answer by DaveA · Dec 01, 2011 at 08:05 PM
You can assign unique names like this, and use Find to get the particular one:
 var nextNameNumber = 0;
 
 function MyFunction ....
 
   enemyClone = Instantiate(enemySpawn, Vector3(27,14.3,-19), transform.rotation);
   enemyClone.name = "enemy"+nextNameNumber;
   nextNameNumber++;
 
               But you may just want to put them into an Array or List to keep track of them without having to use Find
DaveA Im having a problem. They are named correctly and stuff but when I kill one enemyClone1 always gets destroyed first even if Im targeting the second one?
$$anonymous$$aybe an array and fix this do you know were I can learn arrays or can you write me a quick snippet for it?
Your answer
 
             Follow this Question
Related Questions
How can I get the x position for the left(and right) of the screen? 2 Answers
NavMesh giving jerky like motion 0 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Make object move in a direction depending on where it spawns? (C#) 1 Answer
How would I create a script that spawns objects more frequently as time goes on? 3 Answers