ScoreUp

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.

You’d be better off posting here: Unity Engine - Unity Discussions, this board is for UIToolkit, you’re using UGUI.

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.

Thank you.

Yes you were right. However, how can I change the raycast property or how to set it to true?

In an image attached to the same game object as your button or any child of it:
5816254--615961--upload_2020-5-7_16-7-46.png

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();
    }

}

the code looks fine. Is scoreText assigned in the inspector? Are there any error logs in the console?

So I assigned the variable. No errors appear in the console.
https://ibb.co/VSGHvHZ

I added the code to the object (button). I included the variable scoreText in the code, as seen in the picture.

your code should work. maybe you stare at the wrong text object.
Or you didn’t explain well enough what “does not work” mean.

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?

https://ibb.co/db07RBC