Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LeeGibson · Jul 13, 2011 at 01:29 PM · instantiateprefabvaluevar

assign different values to prefabs on instantiation

i have a list[] of strings and a number 'j' how many strings there are.

the instantiation of my prefabs uses

for(i=0;i<j;i++; {instantiate(prefab);}

to create exactly as many prefabs as there are strings; that part is already working.

now what i need to do is give the prefabs a value to distinguish them - preferably a string or a int (i), but i'm stuck at the part where to put it.

i added a script to the instantiation with a static variable, but of course it gets overwritten with the other instantiations so that every prefab has the same value in the end.

i also tried to just name the prefab like the string with

myPrefab.name = "String", but for some reason Unity still puts a (clone) behind it, which renders the string useless (unless i find a method to cut the (clone) from the string afterwards, but that seems to be a bit long-winded).

would be very grateful for a hint to the right direction because i'm running out of ideas right now.

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

2 Replies

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

Answer by aldonaletto · Jul 13, 2011 at 01:52 PM

Instantiate the prefab clone, then rename it:

   var newObj: GameObject;
 
   for (i=0;..;..){
     newObj = Instantiate(thePrefab...);
     newObj.name = "prefab#"+i;
   }
Comment
Add comment · Show 8 · 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 LeeGibson · Jul 13, 2011 at 02:01 PM 0
Share

i already tried that; strangely Unity puts (clone) behind the name, which renders the string useless (unless i find a method to cut the (clone) from the string afterwards)...

the code is

 newObj.name = List[i];

which unity then names List1(clone)

avatar image aldonaletto · Jul 13, 2011 at 11:03 PM 1
Share

I've just tested it and everything worked as expected: my objects were named prefab#0, prefab#1 etc. Unity appends "(clone)" to any object created by Instantiate. Something in your code should be wrong to give these results - it seems you were instantiating the name "List1" too...

avatar image LeeGibson · Jul 14, 2011 at 08:21 AM 0
Share

yeah i suspected that there had to be something wrong, but i'm unable to find the fault in my code. this is the code 'as is':

     var myShark : GameObject;
     private var i : int = 0;
 
 function Start ()
 {    
     var j: int = XmlReader1.j;
 
     for (i=0; i<j; i++)
     {
         var pos = Vector3(Random.Range(136,140),Random.Range(-5,5),Random.Range(99,103));
         Instantiate(myShark,pos, Quaternion.identity);
         Debug.Log("i did something! " +i);
         myShark.name = XmlReader1.testList[i];
     
     }
 }
avatar image Waz · Jul 14, 2011 at 01:08 PM 1
Share

You're modifying the name of the original, not the one returned from Instantiate.

avatar image aldonaletto · Jul 14, 2011 at 04:18 PM 1
Share

The ideal way is to assign the object instantiated to a variable, then rename it, like below (Find will waste a lot of time!):

     var myShark : GameObject; // this is your prefab
     private var shark : GameObject; // this will be instantiated
     private var i : int = 0;
 
 function Start ()
 {    
     var j: int = XmlReader1.j;
 
     for (i=0; i<j; i++)
     {
         var pos = Vector3(Random.Range(136,140),Random.Range(-5,5),Random.Range(99,103));
         shark = Instantiate(myShark,pos, Quaternion.identity);
         // this shark is the one you've instantiated:
         shark.name = XmlReader1.testList[i];
     }
 }
Show more comments
avatar image
0

Answer by Waz · Jul 13, 2011 at 01:33 PM

Put a script on the prefab, with a variable you set:

 myPrefab.GetComponent.<MyScript>().myVariable = "String";
Comment
Add comment · Show 1 · 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 LeeGibson · Jul 13, 2011 at 01:46 PM 0
Share

thanks for your fast answer! unfortunately all the prefabs have the same string now, but i need it to be different strings/values.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

Network instantiate command do not execute in all client.. 1 Answer

Scripts of instantiated objects 1 Answer

How to have unique instances of script on multiple instances of prefab? 1 Answer

Instantiate Problem 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