Display Variable with UI Text
I created a new UI>Text object and named it 'Player1Stamina'. This text is supposed to constantly display the value of the variable 'stamina' found in the script 'player1Movement'.
The problem is I cannot get the text displayed during Play to say anything other than the line of placeholder text I put in the text field on the 'Player1Stamina' which is currently 'Player 1 Stamina'.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class player1Movement : MonoBehaviour {
public Text Player1Stamina;
private int stamina;
int maxStamina;
int staminaRecharge;
bool staminaRecharging;
bool canMove;
void Start ()
{
maxStamina = 100;
stamina = 100;
SetStaminaText ();
staminaRecharge = 2;
staminaRecharging = true;
canMove = true;
}
void Update ()
{
if (staminaRecharging = true && stamina < maxStamina)
{
stamina += staminaRecharge;
SetStaminaText ();
}
if (Input.GetKey ("space"))
{
stamina -= (staminaRecharge * 5);
SetStaminaText ();
}
if (Input.GetKey ("z"))
{
stamina += (staminaRecharge);
SetStaminaText ();
}
}
void SetStaminaText ()
{
Player1Stamina.text = "Stamina: ";
}
}
Answer by TheMrAngel · Mar 11, 2016 at 10:56 AM
I think it misses to add the variable stamina to the Player1Stamina.text. It should look like this:
Player1Stamina.text = "Stamina: " + stamina;
Answer by holymotheroftod · Mar 11, 2016 at 05:05 PM
I must be doing something else wrong in addition to that @TheMrAngel, because the UI text on screen for Player1Stamina does not change unless I edit the text box in it's Text component.
DId you tried to call the Text component?:
Player1Sta$$anonymous$$a.GetComponent<Text>().text = "Sta$$anonymous$$a: " + sta$$anonymous$$a;
Your answer
Follow this Question
Related Questions
Variable from another script isn´t updating 3 Answers
UI Text Not Updating 1 Answer
Best way to simplify this code 1 Answer
Unity 5 Accessing other scripts problem 2 Answers