- Home /
How to create a clone of an object with a different name
Hi guys, Im making a list of my gameobjects and im trying to add a number identifier to the end of each new objects name. So the first object created would be "Enemy" the 2nd object would be "Enemy1", third would be "Enemy2" etc. I have provided the code i have so far below. The Enemy Object is what is being cloned. and where I have enemyList.add(Enemy) I want to make it something like enemyList.add(Enemy + intToString(enemySpawned)) or something. Thanks guys,
public class EnemySpawn : MonoBehaviour {
public GameObject Enemy; //creates the enemy game object
// Use this for initialization
public int enemyCountSpawned = 0;
public static List<GameObject> enemyList = new List<GameObject>();
void Start () {
// enemyList.Add(GameObject.Find ("Enemy")); //this adds the initial enemy to the list
StartCoroutine (EnemySpawner());
}
IEnumerator EnemySpawner(){
while (enemyCountSpawned<3) {
int randomNumber = Random.Range (0, 4);
// print ("the random number for unit spawning is" + randomNumber);
switch (randomNumber) {
case 0:
Instantiate (Enemy, GameObject.Find ("SpawnPointLeft").transform.position, Quaternion.identity);
break;
case 1:
Instantiate (Enemy, GameObject.Find ("SpawnPointRight").transform.position, Quaternion.identity);
break;
case 2:
Instantiate (Enemy, GameObject.Find ("SpawnPointTop").transform.position, Quaternion.identity);
break;
case 3:
Instantiate (Enemy, GameObject.Find ("SpawnPointDown").transform.position, Quaternion.identity);
break;
}
enemyList.Add(Enemy);//adds the enemy to the list
yield return new WaitForSeconds(5f);
enemyCountSpawned++;
}
}
// Update is called once per frame
void Update () {
}
}
Answer by Tanshaydar · Jun 14, 2014 at 01:43 AM
GameObject Enemy1 = Instantiate (Enemy, GameObject.Find("SpawnPointDown").transform.position, Quaternion.identity);
Enemy1.name = "Enemy1";
That should work.
@Tanshaydar I think your method would work, but I already figured it out using a prefab. Anyways, Ill give you the correct answer. Thanks!
Your answer
Follow this Question
Related Questions
Instantiated Object's Scripts not enabled? Not sure why 3 Answers
Adjusting Rotation of Instantiated Objects 0 Answers
All subsequent instantiated objects = auto-named with suffix (Clone)? 1 Answer
Trying to Clone 1 of 5 GameObjects in a array 1 Answer
How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer