【发布时间】: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