Home > OS >  Can't use % in an if statement because it gives me error cs0029
Can't use % in an if statement because it gives me error cs0029

Time:01-29

I wanted to make so that if a the variable speedPoints is a number divisible by 10, my variable moveSpeed would have gotten up by 1 however, when i use the % operator to determine if speedPoints is a multiple of 10 in my if statement it gives me error CS0029. What can i do to fix it?

The error is in line 26 where I added a comment.

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;
    }
}

CodePudding user response:

The modulo operator will return an int but an if statement needs a bool i.e. either true or false. And, as the error message tells you, you can't implicitly convert from an int to a bool.

Essentially what you need to do is create an expression that evaluates to a bool. What that expression is depends on the use case but in case you want to check if speedPoints are divisible by 10 without remainder (without remainder is the important part in that sentence) you could use the following.

if (logic.speedPoints % 10 == 0)
{
    moveSpeed = moveSpeed   1;
}

CodePudding user response:

The if statement condition using logic.speedPoints modulus 10 will always evaluate as true because the outcome will never be zero. To correct this, consider using the equality operator to check for divisibility by 10.

if (logic.speedPoints % 10 == 0)
    {
        moveSpeed  =   1;
    }
  •  Tags:  
  • c#
  • Related