检查了脚本但仍然没有解决
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scoreboard : MonoBehaviour
{
public GameObject[] stars;
private int coinsCount;
void Start()
{
coinsCount = GameObject.FindGameObjectsWithTag("coin").Length;
}
public void starsAcheived()
{
int coinsLeft = GameObject.FindGameObjectsWithTag("coin").Length;
int coinsCollected = coinsCount - coinsLeft;
float percentage = float.Parse( coinsCollected.ToString()) / float.Parse(coinsCount.ToString()) * 100f;
if (percentage >= 33f && percentage < 65)
{
stars[0].SetActive(true);//one stars
}
else if (percentage >= 65 && percentage < 76)
{
stars[0].SetActive(true);
stars[1].SetActive(true);// two stars
}
else (percentage>= 76)
{
stars[0].SetActive(true);
stars[1].SetActive(true);
stars[2].SetActive(true);// three stars
}
}
}
Run Code Online (Sandbox Code Playgroud)
一个else语句不能有一个条件,因此else (percentage >= 76)是无效的。
改用这个:
else
{
stars[0].SetActive(true);
stars[1].SetActive(true);
stars[2].SetActive(true);// three stars
}
Run Code Online (Sandbox Code Playgroud)
将导致代码针对与任何先前if...else if语句不匹配的任何条件运行。
但看起来你只需要另一个else if:
else if (percentage >= 76)
Run Code Online (Sandbox Code Playgroud)