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 /
avatar image
0
Question by CrazyNinja2000 · Jun 28, 2015 at 05:14 PM · booleanif-statementselse

If statement always true

I am working on pong, and depend on different parts of the paddle it hits, different directions it will go. I am going to use integers, but for now im just going to a Boolean to test the x axis movement. depends on if d is true or not, it will go left or right. for some reason, it always does what ever the if statement says, and nothing what the else statement says(even if the if statement is false). i tried switching statements, but that doesn't work.

 using UnityEngine;
 using System.Collections;
 
 public class BallMovement : MonoBehaviour {
     public float x;
     public float y;
     public bool d = false;
     Transform tf;
     void Start () {
         tf = GetComponent<Transform>();
     }
     void Update () {
         Move ();
     }
     void Move(){
         if (d == true) {//i also tried just "if (d)", but still the same movement
             x = x + 0.1f;
         } else {
             x = x - 0.1f;
         }
         Vector2 v = new Vector2 (x,y);
         tf.position = v;
     }
 }
Comment
Add comment · Show 1
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 RLin · Jun 28, 2015 at 05:20 PM 0
Share

Try a debug

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Gravn · Jun 28, 2015 at 05:25 PM

Your d Boolean is public, which means other sources can modify it, perhaps the editor is overriding its value. In the editor in sidebar at your object under your Ballmovement component, should be a box that indicates if its true or not. Untick this if its ticked. Otherwise, add "d = false;" in the Start method in your scrip to ensure its false :)

Another thing i want to add, and this is subjective, is that instead of x = x+0.1f, you can also just write x+= 0.1f; this has the same effect, but might be easier to read.

Lastly, you might want to make the speed dependent on your frames per second, so the ball always has the same speed on different matchines. You can do this by writing x+x+speed*Time .deltaTime or simply x+=speed*Time.deltaTime;

I hope this was somewhat helpful. Good luck :)

Comment
Add comment · Show 1 · 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 starikcetin · Jun 28, 2015 at 05:30 PM 0
Share

The correct answer with a clear explanation.

avatar image
0

Answer by tanoshimi · Jun 28, 2015 at 05:21 PM

d is a public variable, so it's initialised to the value set in the inspector, which I'm guessing is "true". You never change it otherwise.

Comment
Add comment · 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
0
Wiki

Answer by starikcetin · Jun 28, 2015 at 05:34 PM

If you use public variables, they will be setted in inspector, not in decleration.

You may either want to initialize things on Start() or to hide you public variables from showing up on inspector like this:

 [HideInInspector] public float x = 3.0f;
  

Also take a look at here: http://unity3d.com/support/documentation/ScriptReference/HideInInspector.html

Best wishes.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using bool function with parameters 1 Answer

How Do I check if a Bool was True a few moments ago 2 Answers

Making a level up button., 2 Answers

How do you trigger a collider to become enabled after the player collider isn't touching it anymore? 4 Answers

If not working as expected. 1 Answer


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