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?