Hello,
I’m doing the Roll a Ball Tutorial in Unity 5. Until the “Displaying Text” part it went pretty well but i can’t find any solution to change the text displayed by UI Texts with a script… (As i’m very new to Unity i’d like to know how to make basic things like this work in Unity 5 and not using GUIText as it is said in the tutorial).
I dug a lot in the documentation, on the forums and on Unity answers but i never managed to do it.
In the inspector it says Text (Script) so i assumed it was a script and searched how to change script variables from other scripts, it didn’t helped a lot so…
Here are the things (highlighted in yellow) i’m trying to interact with:
here is my script (attached to the Player game object) as it is right now and i think that the only thing missing in it is the damn method i’ve been searching everywhere these last three days.
using UnityEngine;
using System.Collections;
public class characterController : MonoBehaviour {
public float speed;
public float sprint;
private int count = 0;
private string Text;
public Component scriptText;
void Start(){
count = 0;
scriptText = GameObject.Find ("/Canvas/CountText").GetComponent ("Text (Script)");
setCountText ();
}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
sprint = Input.GetAxis ("Sprint");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
if (sprint!=0) {
speed = 1500;
GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
speed = 500;
}
else{
GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "pickup") {
other.gameObject.SetActive (false);
count++;
setCountText();
}
}
void setCountText(){
string counts = count.ToString ();
Text = "Count: " + counts;
// change the text in the UI script Text
}
}