Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Treven · Mar 21, 2015 at 06:42 PM · colliderbooleanplayer movementfalsetrue

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");
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Treven · Mar 21, 2015 at 10:50 PM 0
Share

okay did that. And thanks for helping me out i thought some if statements were not supposed to have brackets.

avatar image tigertrussell · Mar 21, 2015 at 11:09 PM 0
Share

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?

avatar image Treven · Mar 21, 2015 at 11:19 PM 0
Share

no but this is going to help for future code

avatar image tigertrussell · Mar 21, 2015 at 11:23 PM 0
Share

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.

avatar image Treven · Mar 21, 2015 at 11:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges