Assets/Script/Scoreboard.cs(33,2): 错误 CS1525: 意外符号“{”

-5 c# unity-game-engine

检查了脚本但仍然没有解决

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)

Joh*_*lay 6

一个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)

  • @Ruzihm 在这种情况下,我不会对答案投否决票,问题仍然是基本的,它本身被否决并正确关闭(但不确定关闭原因)。答案在被宣布为题外话之前就已经存在了,而且本身并没有错。但这当然只是我的意见。 (2认同)