display counter on screen
Hello, i know there are many of solutions to this topic, but i can't do it in c#. I want to show a counter on a text in screen but i only have GuiText to code on C#, the object i have on screen is UI>Text. Thanks for help.
Answer by PersianKiller · Sep 13, 2017 at 01:45 PM
hey dude attach it to your UI text.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class counter : MonoBehaviour {
public Text t1;
public float timer;
// Use this for initialization
void Start () {
t1 = GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
t1.text = " counter : " + timer;
}
}
I did it. I attached this to the ui text on the canvas. Now I must solve the original question. I don't know but i had to make a change on your code...
void Update () { timer -= Time.deltaTime; timertext.text = timer.ToString(); }
withot .ToSring(); the compiler give me error.
Really thanks! I will accept your answer because it guides me to resolver my problem!!!
np dude,some times ideas are better than the answers :).
Answer by AGENT_GAMER · Sep 13, 2017 at 02:10 PM
Thanks a lot, i didn't call UI library, still it seems that my code doesn't work because "object reference not set to an instance of an object". I have a text con screen and cube with a collider when i touch the collider the counter should count....the script is attached to the cube.
then you can just active the text when ever you want !!!
like this
public GameObject text;
if(you touch the collider){
text.SetActive(true);
}
and about this "object reference not set to an instance of an object"
maybe you didnot assign the text correctly or your object doesn't have a text component.
:)
Your answer
Follow this Question
Related Questions
Please help me to solve this in unity C# scripts (TriggerCounter and OnTriggerEnter) 0 Answers
How can I display the number of times a gameobject has been clicked? 1 Answer
Why do I keep getting a null reference? Its not null! 0 Answers
How To Make A Kill Counter 1 Answer
How to create a step counter. 0 Answers