- Home /
Text shows only when i exit playmode?
I am making an infinite runner game, and player is able to collect coins. This is my script attached to the coins:
using UnityEngine; using System.Collections;
public class PickUpScript : MonoBehaviour { public GameObject MoneyText;
void Update()
{
transform.Rotate (new Vector3 (0, 30, 0) * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
Debug.Log ("pick up picked up");
MoneyText.GetComponent<MoneyScript>().PickedUp ();
gameObject.SetActive (false);
}
}
And this is my script attached to MoneyText GameObject:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class MoneyScript : MonoBehaviour { public float money; public Text moneyText;
public void PickedUp()
{
Debug.Log ("asd");
money = money + 1;
SetMoneyText ();
}
void SetMoneyText()
{
moneyText.text = "Money: " + money.ToString ();
}
}
Ok, those are really simple scripts, but that float money doesn't increase during playmode, it increases when i exit playmode. I want it to increase during playmode, and i don't get what could be the problem here. :)
Thanks :)
$$anonymous$$aybe it might have to do something with that the $$anonymous$$oneyText gameobject is also a prefab?
It depends, did you instanciated it in the Scene ? And did you reference the Text GameObject in the moneyText field ?
Forget about the prefab for now just use a GameObject in your scene, and then -when it works- add it to your project as a prefab.
I didn't instantiate it. And i did reference it. But i solved the problem :)
Answer by Enyph-Games · Jan 25, 2016 at 07:23 PM
I solved the problem myself. If someone wants to know what was the problem, it was that, because moneyText was public, i had to assign it in the inspector, and i assigned the prefab. So, when i played the values did change, but not in the MoneyText gameobject, they changed in MoneyText prefab, and then when i exit playmode, the gameobject takes the values from the prefab. Bye :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why does RectTransform.position ignore the scaleFactor of the canvas? 0 Answers
When I pop-up the Canvas using keyboard, That code doesn't operate.(Unity 5) 0 Answers
All compiler errors have to be fixed, but no errors? 1 Answer