Home > Net >  Can you change the value of a bool in an if function which is activated if the bool is true?
Can you change the value of a bool in an if function which is activated if the bool is true?

Time:10-09

if (Input.GetKeyDown(KeyCode.Space))
        {
          if (jumpcheck = false)
        {
           bool jumpcheck = true;
            gameObject.GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
            yield return new WaitForSeconds (2);
bool jumpcheck = false;

        }

The bool is the variable named "jumpcheck" What was supposed to happen was, if the space key is pressed, check if the variable jumpcheck is false, if it is false, set jumpcheck to true, execute the jump command, then set the jumpcheck variable back to false after 2 seconds.

Here is all of the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class jeff : MonoBehaviour
{
    public float movespeed = 5f;
    public float jumppower = 10f;
    
    

    // Start is called before the first frame update
    void Start()
    {
    bool jumpcheck = false; 
    }

    // Update is called once per frame
    void Update()
    {
       
       StartCoroutine(Test ());
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position  = movement * Time.deltaTime * movespeed;
    }
    


IEnumerator Test ()
{
    
     if (Input.GetKeyDown(KeyCode.Space))
        {
          if (jumpcheck = false)
        {
           bool jumpcheck = true;
            gameObject.GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
            yield return new WaitForSeconds (2);
            bool jumpcheck = false;


        }
         
    
        

        }
}
}

I am getting multiple errors Assets/jeff.cs(34,15): error CS1525: Invalid expression term 'bool'

Assets/jeff.cs(34,20): error CS1026: ) expected

Assets/jeff.cs(34,37): error CS1002: ; expected

Assets/jeff.cs(34,37): error CS1513: } expected

CodePudding user response:

You should make your code more like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class jeff : MonoBehaviour
{
public float movespeed = 5f;
public float jumppower = 10f;

bool jumpcheck;



// Start is called before the first frame update
void Start()
{
    jumpcheck = false; 
}

// Update is called once per frame
void Update()
{
   
   StartCoroutine(Test ());
    Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    transform.position  = movement * Time.deltaTime * movespeed;
}



IEnumerator Test ()
{

 if (Input.GetKeyDown(KeyCode.Space))
    {
      if (jumpcheck == false)
    {
       jumpcheck = true;
        gameObject.GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
        yield return new WaitForSeconds (2);
        jumpcheck = false;


    }
     

    

    }
 }
}

CodePudding user response:

First of all as per your implementation, this is of no use, because it local to Start method.

void Start()
{
    bool jumpcheck = false; 
}

And compilation error is due to this line, there has to be == not = in if condition

if (jumpcheck = false) //wrong

CodePudding user response:

Hello and congrats your first question on StackOverflow. I think your game code logic need some improvement also I edited your code so that it won't go on any compiler error

  • You was declaring jumpcheck bool variable every time you was using it in the same scope(which is error), what you needed was one declaration on a class level and declaration time assignment with value
  • In C# when comparing two values with each other we use == operator for that, and = for value assignments(see more about comparison operators here
  • Also I modified your code so that your logic should be same but more likely bug free, Coroutines are not consistent with Update and if you are running Coroutine every frame in update you will likely end up with performance issues. So I rewrote code such that Coroutine only handles timing so it will be called only after 2 seconds at the worst case
  • And finally I humbly ask you to follow common C# naming convention, I modified some variable names and also class name from lowerCase jeff to PasCalCase Jeff. So here is C# naming conventions

And finally here is the code with all mentioned applied

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Jeff : MonoBehaviour
{
    public float movespeed = 5f;
    public float jumppower = 10f;

    private bool jumpCheck = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && !jumpCheck)
        {
            gameObject.GetComponent<Rigidbody2D>()
                 .AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
            jumpCheck = true;
            StartCoroutine(ResetJumpCheck());
        }

        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position  = movement * Time.deltaTime * movespeed;
    }

    IEnumerator ResetJumpCheck()
    {
        yield return new WaitForSeconds(2);
        jumpCheck = false;
    }
}
  • Related