Hello
I’ve created a short code that doesn’t work for some reason.
Here is the code:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ScoreUp : MonoBehaviour
{
public int score = 0 ;
public Text scoreText;
public void AddScore()
{
scoreText.text = score.ToString();
score++;
}
}
I created a gameobject that has a component Button. I set onClick () to this function, but unfortunately I have no idea what might be wrong.
That aside though, your code should work (the score text will always be 1 too low though as you’re setting the text before you increase the value of score, that might be intentional?) Check the stuff in the editor - make sure your button is has raycastTarget set to true etc, make sure there’s no (maybe invisible) UI element in front of the button that could be blocking the raycast.
I changed the settings. Know why when I use the AddScore () function in the code below after clicking on the object where Button () components are added, it doesn’t work. ?
public class ScoreUp : MonoBehaviour
{
public int score = 0 ;
public Text scoreText;
public void AddScore()
{
score++;
scoreText.text = score.ToString();
}
}
I changed the settings to the “ready” button visible in the photo, after clicking on it the point is added to scoreText. It is set on the top of the screen (in the panel). At the bottom I posted an arrow indicates it has a small square in which the game objects are located (including the one that should be responsible for adding points). Could it be because it is not in the panel?