- Home /
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?
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?
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.
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

Follow this Question
Related Questions
Why is it important to create an empty gameobject for my prefabs? 0 Answers
Replacing empty gameobjects with prefabs 3 Answers
Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer
Why do some objects freeze on creation? 1 Answer
How can I tell what prefab a GameObject belongs to? 2 Answers