- Home /
I can't print a int.ToString() variable in a guiText.text
Well, im making a game were in a scene the player must to collect 9 items, when the player its on the trigger of the item, a texture will be showed in the gui (that works fine) but, the script have two int variables (the first counts the number of items collected, and the second variable equals to the number of items that the player have to collect to win) i want to print the vaule of the variable that counts the number of items collectedina Gui text gameobject, i did something like in the example, but the problem its that the GUI Text cannot change (when I pick an item, the GUI Text still says "0") ¿How I can solve this? ( I am very noob programming ,I still learning)
#pragma strict
//principal variables//
var itlcount : int = 0;
var maximumitl : int = 9;
//textures variables//
var vpickwhitetexture : Texture;
//other variables//
var PlayerTriggesInITL : boolean = false;
var vglobalcounter : GameObject;
function Start () {
vglobalcounter = GameObject.Find("GlobalITLCounter");
}
function OnTriggerEnter (other : Collider) {
PlayerTriggesInITL = true;
}
function OnTriggerExit (other : Collider) {
PlayerTriggesInITL = false;
}
function OnTriggerStay (other : Collider) {
if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) ) {
itlcount += 1;
Destroy(hit.collider.gameObject);
}
}
function OnGUI () {
if (PlayerTriggesInITL == true){
GUI.DrawTexture(Rect(10,10,100,100), vpickwhitetexture, ScaleMode.ScaleToFit, true, 1.0f);
}
}
//counter//
if ( itlcount < maximumitl ){
vglobalcounter.guiText.text = itlcount.ToString () ;
}
else {
vglobalcounter.guitext.text = "All items collected" ;
}
Answer by Asteiros · May 26, 2014 at 10:43 AM
place your code inside of some function and call it wherever you need. In OnTriggerStay inside of IF statement. I mean this code:
if ( itlcount < maximumitl ){
vglobalcounter.guiText.text = itlcount.ToString () ;
}
else {
vglobalcounter.guitext.text = "All items collected" ;
}
Your answer
Follow this Question
Related Questions
string + int = variable 2 Answers
Text UI is not being assigned 2 Answers
[4.6 UI] Show text as an int variable error 2 Answers
Converting a single String character to representative int. How? Javascript 2 Answers
Check if a guiText = a number? 2 Answers