Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by camtronius · Jun 14, 2014 at 01:39 AM · gameobjectinstantiateclonertsname

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 () {
     
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image camtronius · Jun 16, 2014 at 03:15 PM 0
Share

@Tanshaydar I think your method would work, but I already figured it out using a prefab. Anyways, Ill give you the correct answer. Thanks!

avatar image Tanshaydar · Jun 18, 2014 at 01:50 AM 0
Share

Glad that you get it working!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges