Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 songxue77 · Jun 10, 2015 at 04:47 PM · gameobjectinstantiateprefabchildgameobject.find

Instantiate prefab with different values each time

Hello, I am new to Unity engine and face a problem in my work..

Sorry for the stupid question and Thanks for your time to read it.

I have a button , by clicking the button I will instantiate a prefab with 4 child gameobject.

I have to set different value of 4 child gameobject each time..

But when I use GameObject.Find() function to set the value when I instantiate, it changes all of the child gameobject's value, because of all prefabs' child gameobject name are same...

I can't figure the solution out, for now.

What should I do? Using a different function or there are some ideas to change the value of child gameobject which is recently instantiated?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by DiegoSLTS · Jun 10, 2015 at 05:41 PM

The "Instantiate" function returns a reference to the object that was created. Just store that reference into a variable and make any change you want:

 using UnityEngine;

 public class Test() {
     GameObject prefab;
 
     void Start() {
         GameObject clone = (GameObject)Instantiate(prefab);
         clone.name = "I'm a clone of the prefab".
         Debug.Log(clone.name);
         GameObject clone2 = (GameObject)Instantiate(prefab);
         clone2.name = "I'm a different clone".
         Debug.Log(clone2.name);
     }
 
 }

Edit: Sorry, I just read you instantiate a prefab with 4 childs, you can iterate over the childs of any object using it's Transform, look here: http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html

Storing the reference after the instantiation of the prefab and iterating over the childs like shown in that link you should be able to change any value of each child.

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 lolmonkeyplayer · Oct 31, 2020 at 03:14 PM 0
Share

yo,i made an account just to say thank you. i fixed all my bugs just by telling us instantiate actually gives you the refference to the instantiated thing....

avatar image
0

Answer by crohr · Jun 10, 2015 at 05:26 PM

Generally speaking it is bad practice to use GameObject.Find() because it kills frame rate if used frequently. The better practice is to maintain a reference to the GameObject. I believe, in your case (correct me if I am wrong) you have one prefab and that prefab has four children, and you want to modify some values in the children. (I am going to assume you are modifying their transforms, but you can add a reference for any Component type.)

The following will allow you to drag and drop the child elements in to the array on the prefab. From there you can modify them however you want in code.

 public class MyObject : MonoBehaviour
 {
     [SerializeField] private Transform[] children; 
 
     public void Start() 
     {
         // Modify each child Transform here ...
     }
 }
 
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
0

Answer by zab · Jun 11, 2015 at 08:54 AM

You can iterate through children of the GameObject.

http://docs.unity3d.com/ScriptReference/Transform-childCount.html http://docs.unity3d.com/ScriptReference/Transform.GetChild.html

Or post your code :)

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
0

Answer by sacredgeometry · Oct 31, 2020 at 04:11 PM

There are design patterns called the factory patterns that are exactly for this:


Unity Implementation:

https://www.patrykgalach.com/2019/03/28/implementing-factory-design-pattern-in-unity/1


More info:

https://www.youtube.com/watch?v=EcFVTgRHJLM&t=1s

https://www.youtube.com/watch?v=v-GiuMmsXj4


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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn Prefabs 1 Answer

Instantiate object as child of exsisitng game object 1 Answer

How to instantiate a prefab with a script attached? 2 Answers

I cant delete multiple instantiated prefabs 0 Answers

Instantiating to gameobject.transform.position 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