• 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
0
Question by bstout · Oct 16, 2016 at 07:04 PM · cloneinstantiate prefabnamerename

properly rename instantiated objects

i am trying to rename the defenders as they spawn so i can test for their name on their death and change their boolean so they can respawn.

however i do not understand the results i am getting

here is the code

 public void Summon()
     { 
         
         string a = "Button";
         string b = "Button(1)";
         string c = "Button(2)";
         string d = "Button(3)";
         string e = "Button(4)";
         string f = "Button(5)";
         string g = "Button(6)";
         string h = "Button(7)";
     
 
         string[] split = thisButton.Split(' ');
     //    print(a.Equals(split[0]).ToString());
         Debug.Log(thisButton);
 
 
         if (a.Equals(split[0]).ToString() == "True" && defender0 == false)
         {
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 270, 0));
             defender.name = "defender0";
             defender0 = true ;
             restartButtonAnim ();
         }
         else if (b.Equals(split[0]).ToString() == "True")
         {
             
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 315, 0));
             defender.name = "defender1";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }
         else if (c.Equals(split[0]).ToString() == "True")
         {
 
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 2, 0));
             defender.name = "defender2";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }
         else if (d.Equals(split[0]).ToString() == "True")
         {
     
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 45, 0));
             name = "defender3";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }        
         else if (e.Equals(split[0]).ToString() == "True")
         {
 
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 90, 0));
             defender.name = "defender4";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }
         else if (f.Equals(split[0]).ToString() == "True")
         {
 
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 135, 0));
             defender.name = "defender5";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }
         else if (g.Equals(split[0]).ToString() == "True")
         {
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 180, 0));
             defender.name = "defender6";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }
         else if (h.Equals(split[0]).ToString() == "True")
     
         {
             Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 225, 0));
             defender.name = "defender7";
             restartButtonAnim ();
             Debug.Log(defender.name);
         }            
 
 
     }

In the Console it is printing out Button 6 and Defender 6 but in the hierarchy the name will be defender3(Clone). The arbitrary number after defender varies and duplicates in a way that seems random to me. any help on naming the clones would be great. Thanks.

Comment
Add comment · Show 1
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 flashframe · Oct 16, 2016 at 07:19 PM 0
Share

Try storing a reference to the object you are Instantiating and changing the name of the reference. eg.

 GameObject g = (GameObject)Instantiate (defender , buttonPos.position  ,  Quaternion.Euler(0, 135, 0));
 g.name = "defender5";

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Stankiem · Oct 16, 2016 at 07:20 PM

I'm sorry and not trying to be rude but your code is pretty scary IMO, Is "defender" your prefab?

Instead do something like this:

GameObject new_defender; new_defender = Instantiate (defender , buttonPos.position , Quaternion.Euler(0, 225, 0)) as object; new_defender.name = "defender6";

Also, I'm not sure why you're parsing a string for this data, can't you figure out a more efficient way of doing this? shouldn't be necessary here unless I'm misunderstanding what you're doing.

Good luck dude

Comment
Add comment · 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
1

Answer by MegaManX_777 · Oct 16, 2016 at 07:38 PM

You seem to be renaming the prefab that you are using to instantiate the defender, instead of using

  Instantiate (defender ,buttonPos.position , Quaternion.Euler(0, 225, 0));
  defender.name = "defender7";
  Debug.Log(defender.name);


You should instead use :

 GameObject defender7 = Instantiate(defender, buttonPos.position, Quaternion.Euler(0, 225, 0));
 defender7.name = "defender7"
 Debug.Log(defender7.name);


Like this, you will edit the name of the object that you instantiated, not the prefab. This applies to all defenders.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Changing material name 14 Answers

Instantiate(Cloning) gives no errors but just does not work 2 Answers

child names of clone dont match model 1 Answer

Instantiate inactive,Making a GameObject active/instantiate 3 Answers

Restore prefab's name? 4 Answers


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