How to link buttons with text,How to link buttons to text

Hello. I’m trying to make a game in Unity where the player can press a button and increase the score by 1. To display the score, I’m using text to display my score. I am puzzled on how to make a code that does this. If anyone can help, that would be great!,

Hi @PanicDevice This is fairly simple to achive. You have to create a custom script for that in some cases and in others you can just use unityevents system. The chosing from one or another depends on what “trigger” the game to modify the score you descrive. If for example what make the score to increase is an enemy that you destroy and then rise the score, you will have to use script mostly. But if is a button that you press or a trigger box that you cross, you can do it within the event system of unity.

For the script approach i can suggest the following.
`
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Score : MonoBehaviour {
public Text scoreUIText;
public int scoreTest;

void Update () {
RefreshUI ();
}

public void RefreshUI () {
scoreUIText.text = “” + scoreTest;
}
}`

This is a very simple test script that i did to show you how its done. You have to create a ui element that has a text and then put that text ui on the script text var wich is “scoreUIText”. Then in the variable “scoreTest” put any integrer that you want to appear on the text field. You can mix string with float and int there if you use the “+” argument.

The second approach wich is using event system. By default ui buttons and other ui elements has event system wich is the box space that appear at the bottom of the button component. It appear wich a keyword like “On Click ()” “list is empty”. But you can use this on a custom script and use them on for example the script that manage the heal for an enemy when is being destroyed you can anything you put in the unityevents. This can be a little more complicated to start with but at then end is far better than using a script that has to be referenced by many others. For example if you are in a game where there is a loots of calls for the score, you can quickly have an overhead becouse many enemys are referencing many other gameobjects and components. So to avoid that is the event system, wich can be used for many diferent ways that im just starting to discover.

Unity Events link

This deserves an entire explanation so i will give you the link where i found it in the first place so yo can read it and understand what is it and what is for.

So for example if where doing like a phone game where you are doing many things fast like a 2d shooter space like allien invaders xD. I whould use the unityevents for that, so when an enemy is destroyed anything you put on the unityevents invoke will be perform very quicly and very easly. In fact im using this for pickups on my shooter game, and for the objective system.