N
N
NagerG2021-04-08 14:40:20
C++ / C#
NagerG, 2021-04-08 14:40:20

How to add one to a variable if some variable becomes zero unity?

Good afternoon! The crux of the matter is this: there is a clicker 606ee9335204b573882148.png. As soon as the taps value - the taps variable becomes zero, you need to add 1 to the score - to the points.

I made this option, but as soon as the taps expression becomes equal to zero, a unit is infinitely added to the score variable:
if (taps == 0)
{
beh.score_1++ ;
}

Tap script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pop_on_touch : MonoBehaviour
{
public Vector2 scale1;
public UnityEngine.UI.Text GoldDisplay;
public uint taps = 50;
private score beh;

void Awake()
{
beh = GetComponent();
}

void Update()
{
if(Input.GetMouseButton(0))
{
transform.localScale = Vector2.Lerp(transform.localScale, scale1, 20.0f * Time.deltaTime);

}
else
{
transform.localScale = new Vector2(6, 6);
}
GoldDisplay.text = "Tap: " + taps;
if (Input.GetMouseButtonDown(0))
{
taps -= 1;

}
if (taps == 0)
{
beh.score_1++;
}

}

}

Score Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class score : MonoBehaviour
{
public UnityEngine.UI.Text ScoreDisplay;
public int score_1 = 0;
void Start()
{

}

// Update is called once per frame
void Update()
{
ScoreDisplay.text = "Points: " + score_1;
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-04-08
@GavriKos

Well, do not check the number of taps in the update. And with tapa.

S
Sergey Vodakov, 2021-04-08
@WaterSmith

Fix like this:

void Update() {
    if(Input.GetMouseButton(0)) {
        transform.localScale = Vector2.Lerp(transform.localScale, scale1, 20.0f * Time.deltaTime);
    } 
    else {
        transform.localScale = new Vector2(6, 6);
    }
    GoldDisplay.text = "Тапов: " + taps;
    if (Input.GetMouseButtonDown(0)) {
        taps -= 1;
        if (taps == 0) {
            beh.score_1++;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question