Cant find text objects after being renamed.
Hello
So i had these two scripts working until i figured out each object has to have a different name for the ObjectTextDisplay script to work correctly. SO i updated the randomize script to give unique names to each object that is created. Now when it comes to updating the ObjectDisplayText script i am gettin errors because the name that the object was initially created with no longer exists. In the Randomize script you can see how they are renamed. My problem starts on line 24 of the ObjectDisplayText script.
ObjText = GameObject.Find ("ObjectText").GetComponent ();
should be somthing like this x being the number of the object in its parented hierarchy
ObjText = GameObject.Find (gameObject.name + "_Obj" + x + "_ObjectText").GetComponent (); ObjText2 = GameObject.Find (gameObject.name + "_Obj" + x + "_ObjectText2").GetComponent ();
Can anyone shed some light on the subject. I hope thats not to convoluted.
Randomize Objects File
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class RandomizeObj : MonoBehaviour {
public List<Transform> ObjList = new List<Transform>();
//public Transform obj;
public Vector3 postiion;
public GameObject objspawned;
public int totalobjs;
private int ObjNum;
public float ObjectsetWidth;
public float ObjectsetLength;
public float respawntime = 3600.0f;
public float respawn = 3600.0f;
private Text ObjText;
private Text ObjText2;
//respawn object if destryed after 60minutes - 3600 Seconds - 3600000.0f milliseconds
void Start () {
int spawned = 0;
while (spawned < totalobjs) {
Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (position, Vector3.down, out hit)) {
ObjNum = Random.Range (0,ObjList.Count);
if ((hit.collider.gameObject.tag != "Tree") && (hit.collider.gameObject.tag != "Stone") && (hit.collider.gameObject.tag != "Bush") && (hit.collider.gameObject.tag == "Roads") && (hit.collider.gameObject.tag != "Debris") && (hit.collider.gameObject.tag != "Pickup")) {
Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + spawned;
ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
ObjText.name = gameObject.name + "_Obj" + spawned + "_ObjectText";
ObjText2.name = gameObject.name + "_Obj" + spawned + "_ObjectText2";
} else {
while ((hit.collider.gameObject.tag == "Tree") && (hit.collider.gameObject.tag == "Stone") && (hit.collider.gameObject.tag == "Bush") && (hit.collider.gameObject.tag == "Roads") && (hit.collider.gameObject.tag == "Debris") && (hit.collider.gameObject.tag == "Pickup")) {
position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
hit = new RaycastHit ();
}
if (Physics.Raycast (position, Vector3.down, out hit)) {
Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + spawned;
ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
ObjText.name = gameObject.name + "_Obj" + spawned + "_ObjectText";
ObjText2.name = gameObject.name + "_Obj" + spawned + "_ObjectText2";
//Debug.Log (hit.collider.gameObject.tag + " " + newObj.name + " Landed on top of object");
}
}
}
spawned++;
}
}
void Update() {
respawntime -= Time.deltaTime;
if (respawntime < 0) {
CheckForObjs ();
}
}
void CheckForObjs() {
int i = 0;
while (i < totalobjs) {
if (objspawned == null) {
Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
ObjNum = Random.Range (1,ObjList.Count);
objspawned = GameObject.Find(gameObject.name + "_Obj" + i);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (position, Vector3.down, out hit)) {
Transform newObj = (Transform)Instantiate (ObjList[ObjNum], hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + i;
Debug.Log ("Not Found Object at " + i);
}
}
i++;
}
respawntime = respawn;
}
}
Object Text Display Script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ObjectTextDisplay : MonoBehaviour {
public string ObjString;
public string ObjString2;
public Text ObjText;
public Text ObjText2;
public float fadeTime;
public float fadeTime2;
public bool displayInfo;
public Vector3 objfwd;
int rayLength = 3;
// Use this for initialization
void Start () {
ObjText = GameObject.Find ("ObjectText").GetComponent<Text> ();
ObjText.color = Color.clear;
ObjText2 = GameObject.Find ("ObjectText2").GetComponent<Text> ();
ObjText2.color = Color.clear;
displayInfo = false;
}
// Update is called once per frame
void Update (){
FadeText ();
if(Input.GetKeyDown (KeyCode.Escape)){
Screen.lockCursor = false;
}
}
void FadeText() {
if (displayInfo) {
ObjText.text = ObjString;
ObjText.color = Color.Lerp (ObjText.color, Color.white, fadeTime * Time.deltaTime);
ObjText2.text = ObjString2;
ObjText2.color = Color.Lerp (ObjText.color, Color.white, fadeTime2 * Time.deltaTime);
} else {
StartCoroutine (ObjCoroutine());
}
}
IEnumerator ObjCoroutine()
{
//This is a coroutine
yield return new WaitForSeconds (20f); //Wait one frame
ObjText.color = Color.Lerp (ObjText.color, Color.clear, fadeTime * Time.deltaTime);
ObjText2.color = Color.Lerp (ObjText.color, Color.clear, fadeTime2 * Time.deltaTime);
displayInfo = false;
}
}
Answer by ReggieBeRetro · Jul 08, 2016 at 01:46 PM
Got it!.. Answered my own question.. XD only took 3 hours
ObjText = GameObject.Find (gameObject.transform.GetChild(0).GetChild(0).gameObject.name).GetComponent<Text> ();
ObjText.color = Color.clear;
ObjText2 = GameObject.Find (gameObject.transform.GetChild(0).GetChild(1).gameObject.name).GetComponent<Text> ();
ObjText2.color = Color.clear;
Your answer
Follow this Question
Related Questions
Mysterious Extra KeyUp Event (5.3.3f1) 2 Answers
Accessing UI text 0 Answers
How do you change UI Text to an int? 4 Answers
NullReferenceException on Text element 1 Answer
How can I use a scribt two combine two objects on collision with fixedjoint? 0 Answers