- Home /
The public bool will not change unless i set it
I'm trying to get my game to able movement when colliding with the ground and no control available when in the air.
The problem:
Ground is not turning to false when off a collider.
Cant get it to say "your off" when i'm off the ground, which should mean i have no control.
The code to stop all movement only works if i change it in the inspector.
I have:
Set print functions to see what is working, this helps to know whats working and whats not.
Added rigid bodies to other objects, did nothing.
Void on awake to say that ground was true or false, did nothing.
Separating movement into its own function, That helped simplify my code.
I've checked all over the site as well as Google and saw that my code should work, but is not.
I attached a Sample of the code for reference:
using UnityEngine;
using System.Collections;
public class PlayerController2 : MonoBehaviour {
public float speed = 1;
public float jump = 1;
public bool Ground;
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.tag == "Enemy")
print ("ow");
if (coll.gameObject.tag == "Ground")
print ("Rollin 'Ground is true'");
bool Ground = true;
}
void OnCollisionExit2D(Collision2D coll){
if (coll.gameObject.tag == "Enemy")
print ("Yay");
if (coll.gameObject.tag == "Ground")
print ("Rollin 'Ground is false'");
bool Ground = false;
}
void Update () {
if (Ground == true) {
Movement ();
}
if (Ground == false) {
print ("Your off");
}
}
void Movement () {
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += new Vector3 (0.0f, jump * Time.deltaTime, 0.0f);
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position -= new Vector3 (0.0f, jump * Time.deltaTime, 0.0f);
}
if (Input.GetKeyDown ("space")) {
speed = speed * 2;
print ("Space is Down");
}
if (Input.GetKeyUp ("space")) {
speed = speed / 2;
print ("Space is Up");
}
}
}
Answer by tigertrussell · Mar 21, 2015 at 07:10 PM
Well there's a bit of code here, but let's start off with this.
You have several calls that are like this:
if (coll.gameObject.tag == "Ground")
print ("Rollin 'Ground is false'");
bool Ground = false;
Based on your indentation, it seems like you think both of these lines will be executed only if your predicate (the statement inside your if(...)
) is true. But that's not the case.
If you choose to omit brackets after an if
-statement, only the next statement will be executed.
That's why it's best practice to always use brackets with an if
-statement.
Start off by changing all instances like the one above to this:
if (coll.gameObject.tag == "Ground") {
print ("Rollin 'Ground is false'");
bool Ground = false;
}
And we'll continue from there.
okay did that. And thanks for helping me out i thought some if statements were not supposed to have brackets.
No problem. Some people prefer using no brackets when there is only one line to be executed, but it is generally safer to always use brackets. Not to mention seldom will you need to execute only a single line of code based on an if
-statement, in my experience. Even if that is the case when you first write it, I find that there is often a reason to go back and add another line of code.
Did this solve your issue?
Have you ever heard of step-debugging? I think it would be perfect for figuring out exactly what is going wrong in your script. Shameless plug: I made a tutorial for this. There are many others, as well.
no but i looked at the video and don't seem to understand it to well. It seems like De-bugging it is just breaking down the code into its simplest elements.
Your answer
Follow this Question
Related Questions
Boolean while looking at a game object? 1 Answer
Boolean values not behaving as expected. Please Help?! 0 Answers
Engine Only Returns Responses for False 2 Answers
Boolean problem!! 2 Answers
Boolean Uncheck Inspector if False 2 Answers