Question by
magicskillz · Nov 06, 2016 at 12:38 AM ·
variabledebug
How do you add a variable to a text?
I can't figure out how to make this into a counter so when i touch an object it adds a point. Can't get it to work. Please Help.
My Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Points : MonoBehaviour {
public Text TextForPoints;
public int AmountOfPoints;
// Use this for initialization
void Start ()
{
TextForPoints.text = AmountOfPoints;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision)
{
AmountOfPoints = AmountOfPoints++;
}
}
Thx :D
Comment
Answer by janus1001 · Nov 06, 2016 at 12:51 AM
You change the text of the Text object only once. You change it only in Start(). You should add this:
TextForPoints.text = AmountOfPoints
In your Update(), or to be more optimized in OnCollisionEnter() function, after you add a point.
Also, it is enough that you write
AmountOfPoints++;
Instead
AmountOfPoints = AmountOfPoints++;
Because this function already changes the value of a variable.