【问题标题】:Can't use % in an if statement because it gives me error cs0029不能在 if 语句中使用 % 因为它给我错误 cs0029
【发布时间】:2023-01-29 04:27:06
【问题描述】:

我想这样做,如果变量 speedPoints 是一个可以被 10 整除的数字,我的变量 moveSpeed 就会增加 1,但是,当我使用 % 运算符来确定 speedPoints 是否是我的 if 语句中的 10 的倍数时,它给出了我的错误 CS0029。 我能做些什么来修复它?

错误在我添加注释的第 26 行。

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class PipeMoveScript : MonoBehaviour
{
    public float moveSpeed = 1;
    public float deadZone = -45;
    public bird_script bird;
    public LogicScript logic;

    // Start is called before the first frame update
    void Start()
    {
        bird = GameObject.FindGameObjectWithTag("Bird").GetComponent<bird_script>();
        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
    }

    // Update is called once per frame
    void Update()
    {
        // here the CS0029 error occurs
        if (logic.speedPoints % 10)
        {
            moveSpeed = moveSpeed + 1;
        }

        if (bird.birdIsAlive == true)
        {
            transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;
        }

        if (transform.position.x < deadZone)
        {
            Destroy(gameObject);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LogicScript : MonoBehaviour
{
    public int speedPoints = 0;
    public int playerScore;
    public Text scoreText;
    public GameObject gameOverScreen;

    [ContextMenu("Increase Score")]
    public void addScore()
    {
        playerScore = playerScore + 1;
        scoreText.text = playerScore.ToString();
        speedPoints = speedPoints + 1;
    }

    public void restartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void gameOver()
    {
        gameOverScreen.SetActive(true);
    }

    public void startGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        Time.timeScale = 1f;
    }
}

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    模运算符将返回 int,但 if 语句需要 bool,即 truefalse

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      相关资源
      最近更新 更多