- Home /
Problems to change text from a child in a child
Hey guys, I have problems to change a text from a child of a child of another object
This is my hierarchy:
My script is attached to the "Schlafsack(Clone)" and I have to change the text from the Tooltip child "Text".
This is the Code I tried but it didn't work.
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;
public class Schlafsack : MonoBehaviour {
SpielerWerte spielerwerte;
float distance;
GameObject hud;
GameObject tooltip;
Text text;
// Use this for initialization
void Start () {
spielerwerte = GameObject.FindGameObjectWithTag("Player").GetComponent<SpielerWerte>();
hud = GameObject.Find("HUD");
text = hud.transform.GetChild(5).GetComponentInChildren<Text>();
//text = tooltip.transform.GetChild(1).GetComponent<Text>();
}
// Update is called once per frame
void Update () {
distance = Vector3.Distance(GameObject.FindGameObjectWithTag("Player").transform.position, this.transform.position);
if(distance < 5){
text.text = "Press E to sleep in Sleepingbag";
if(Input.GetKeyDown(KeyCode.E)){
spielerwerte.sleep = true;
}
}
}
}
Thanks for help :)
Answer by PabloJMartinez · Jun 30, 2015 at 04:43 AM
GetChild index starts at 0, not 1, so you should use GetChild(4) in order to be able to access Tooltip.
Your answer
Follow this Question
Related Questions
How to use special symbols 1 Answer
,InputField backspace returns already send text 0 Answers
What's your equivalent of old GUIStyle ? 0 Answers
How to measure the width of a string? 0 Answers