• 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 g0t0_wasd · May 16, 2015 at 03:50 AM · gameobjectprefabinstantiation

How to find a specific clone of a prefab and access its children

I have a prefab which has 3 child text objects. How to access it in clones? GameController script:

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
 
     public GameObject drop;
     public Vector3 spawnValues;
 
     public float spawnWait;
     public float startWait;
     public float waveWait;
 
 
     // Use this for initialization
     void Start () {
         StartCoroutine(Rain());
     }
     
     IEnumerator Rain()
     {
         yield return new WaitForSeconds(startWait);
         while(true){
             for (int i = 0; i < 5; i++){
                 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x),
                                spawnValues.y, spawnValues.z);
                 Instantiate (drop, spawnPosition, Quaternion.identity);
                 yield return new WaitForSeconds(spawnWait);
             }
             yield return new WaitForSeconds(waveWait);
         }
     }
 }

And script attached to prefab:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class DropFaller : MonoBehaviour {
 
     float speed;
     Vector3 movement;
 
     int number_1;
     int number_2;
     string sign;
 
     TextMesh nOneText;
     TextMesh nTwoText;
     TextMesh signText;
 
     void Start(){
         nOneText = GameObject.FindWithTag("number_1").GetComponent<TextMesh>();
         nTwoText = GameObject.FindWithTag("number_2").GetComponent<TextMesh>();
         signText = GameObject.FindWithTag("sign").GetComponent<TextMesh>();
 
         number_1 = Random.Range(1, 100);
         number_2 = Random.Range(1, 100);
         sign = "+";
         nOneText.text = number_1.ToString();
         nTwoText.text = number_2.ToString();
         signText.text = sign;
 
         speed = Random.Range(-0.06f, -0.01f);
         movement = new Vector3(0f, speed, 0f);
     }
     
     void FixedUpdate () {
         gameObject.transform.position += movement;
     }

}

But this code on each instantiation changes text for same object instead of setting to new clones. I tried to change GameObject to gameObject, or use GameObject.gameObject - nothing works. Is there way to access specific instance, something like self or this or whatever? Or should I use arrays or a hashtables?

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 g0t0_wasd · May 17, 2015 at 02:51 PM 0
Share

Ok, maybe there is some misunderstanding. Well what I want to do is to spawn a prefab which has few child GameObjecs and at least one of this children has a text mesh with random text on it.

 GameObject |
            |> GameObject
            |> GameObject
            |> GameObject |
                          |> Text$$anonymous$$esh

How to do this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lylek · May 16, 2015 at 06:28 AM

Don't use GameObject.Find. There is no way of specifying which gameObject it will get unless there is only one of them in the scene.

Instead, search the instantiated prefab itself for its children. You can do:

 private GameObject[] children;

 void Start () {
     children = GetComponentsInChildren<GameObject>();

     foreach(GameObject child in children) {
         if(child.tag == "number_1") {
             nOneText = child.GetComponent<TextMesh>();
         }
         else if(child.tag == "number_2") {
             nTwoText = child.GetComponent<TextMesh>();
         }
         else if(child.tag == "sign") {
             signText = child.GetComponent<TextMesh>();
         }
     }
 }

Though, you may not even need three TextMeshes, if you do:

 nOneText.text = number_1.ToString() + sign + number_2.ToString();

Also, if you use a Transform variable, you can simple drag and drop your child object to assign it in the editor, and not have to search for it at all.

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 g0t0_wasd · May 16, 2015 at 12:49 PM 0
Share

Well I'm mostly concerned on accessing specific prefab and just then its children.

And thank you for Idea with 1 Text$$anonymous$$esh!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why is it important to create an empty gameobject for my prefabs? 0 Answers

Help? Im trying to get my zombie prefab to spawn, but stop them spawning after a set spawn limit. 1 Answer

Instantiate Prefab by unique ID instead by string 0 Answers

Reinitialize prefab 0 Answers

How to save/load references to components and prefabs 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