K
K
KorNorJb2021-12-11 11:24:38
Unity
KorNorJb, 2021-12-11 11:24:38

How to make a score counter and turn off button?

Hello, I'm new to Unity and need help! I want to add a score counter. When the scale is filled (for example, up to 100), then 1 point is awarded and the button is disabled until the scale reaches zero, and when it has reached, the button is turned on. And when the score counter reaches, for example, 20 points, the inscription "You won!" will be displayed. Please help me implement this in code. Here is the code I have at the moment:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class ClickScript : MonoBehaviour {
 
   
    public Image clickMeter;
    public float curMeter, maxMeter;
    public float incMeter;
    public float decMeter;
    public float meterReduceTimer;
    public float timeBetweenClicks;



    void Update () {
        ImageChange();
        MaxMinValue();
        Clicking();
        ReduceMeter();
    }
 
    public void Clicking()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
                curMeter += incMeter;
                meterReduceTimer = 0;
            
        }
    }
 
    void ReduceMeter()
    {
        meterReduceTimer += 1;
        if(meterReduceTimer > timeBetweenClicks)
        {
            curMeter -= decMeter;
        }
    }
 
    void ImageChange()
    {
        clickMeter.fillAmount = curMeter / maxMeter;
    }
 
    void MaxMinValue()
    {
 
        if(curMeter < 0)
        {
            curMeter = 0;
        }else if(curMeter > maxMeter)
        {
            curMeter = maxMeter;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
stasersmailov, 2021-12-11
@stasersmailov

I didn’t quite understand how the points themselves will be awarded, but you can add a slider for visualization, and in your script just make the OnClick() function (if points are awarded per click), make a variable in the class that will store this value and then write OnScoreChanged() where it will already be checked if it is equal to 100 then, for example, add a unit to the _score variable and after adding it check >= whether it is 20

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question