Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

unity3d - How can I disable a button after when counter hits zero? C#,Unity

I am implementing a feature that my button should only spawn a limited amount of objects.

I put a counter on the button so that it shows how many clicks are remaining until it reaches 0.

Then the count stops but the button is still active.

How can I disable the button after the count reaches 0, so that one can not spawn anymore objects.

I am using the code below to reduce the count.

Thanks for the help.

private TMP_Text _spawn;
private int _spawnAmount;

void Start()
{
    _spawnAmount = 2;
    _spawn = GetComponent<TMP_Text>();
}

void Update()
{
    _spawn.text = _spawnAmount.ToString();
}

public void ReduceSpawnAmount()
{
    if (_spawnAmount > 0)
    {
        _spawnAmount--;
    }
    else
    {
        return;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might just want to make it non-interactable.

GetComponent<Button>().interactable = false;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...