I am just starting to write C# and i would use Javascript if i could because my knowledge about C# specifically is “small”.
So, i have a UI Text, and i am trying to make it add 1 to the score each time the player clicks a button. For example, the score is 0. The player clicks the button, and it is changed to one. If he clicks again, it changes to two and it continues. I have tried A LOT OF THINGS but i always got myself in the same two problems: i can’t reference something or i can’t understand how it works so i can make it. I think i am in the right path, but i couldn’t really find anything related to how to reference texts and how to make a variable for it, and i am also struggling in making a trigger for it.
Here is my code for now:
using UnityEngine;
using System.Collections;
public class ObjClick : MonoBehaviour
{
bool click = false;
public GameObject fall;
public GameObject txt; //I wanted to use this variable to hold the text, but as i said i couldn't reference it. There isn't exactly something like "public Text txt".
public Animator anim;
public int score;
void Start()
{
fall.SetActive(false);
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("Action");
fall.SetActive(true);
//I plan to make something like, on left mouse click, add one to the score.
}
}
void UpdateScore()
{
txt.text = score; // <- This is my main problem. I don't know how to reference the text. I always get the error "Type 'UnityEngine.GameObject' does not contain a definition for text" etc...
}
}
Could someone help me to finish this? It’s very important. Any help would be appreciated. Btw this is my last question.