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 s.stouby · Feb 24, 2015 at 10:50 AM · if statement

IF statement not working?

Hey everybody. Im trying to make a pick-up-and-drop script. So far everything works great, except for one thing. I'm trying to limit the items you can pick up to a single item at a time. So the player is only able to pick one item, and then having to drop the item before picking up a new one. Only problem is that the IF statement doesn't give a crap about the rules it was suppose to follow ? ? ?

public class grab : MonoBehaviour {

     public Transform playerH;
     public bool handsfull = false;
     public bool guigrab = false;
     public bool cangrab = false;
     private int buttonWidth = 200;
     private int buttonHeight = 50;
     
     public void OnTriggerEnter (Collider collision)
     {
         if (collision.gameObject.tag == "Player") 
         {
             cangrab = true;
         } 
     }
     public void OnTriggerExit (Collider collision)
     {
         if (collision.gameObject.tag == "Player") 
         {
             cangrab = false;
             guigrab = false;
         } 
     }
     void OnGUI()
     {
         if(guigrab)
         {
             GUI.Box (new Rect (0, 0, buttonWidth, buttonHeight), "Right click to pick up object");
         }
     
     }
     
     void FixedUpdate () {
         if (cangrab == true)
         {
             guigrab = true;
             if(Input.GetKey(KeyCode.Mouse1) && handsfull != true)
             {
                 this.transform.parent = playerH;
                 this.transform.localPosition = new Vector3(0,0,0);
                 this.rigidbody.isKinematic = true;
                 guigrab = false;
                 handsfull=true;
             }
 
             if (handsfull == true && Input.GetKey (KeyCode.Mouse1) && Input.GetKey(KeyCode.Mouse0))
             {
                 handsfull = true;
                 this.transform.parent = null;
                 this.rigidbody.isKinematic = false;
             }
         }
     }
     
 }

Im greatfull for any kind of help !

Comment
Add comment · Show 4
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 gjf · Feb 24, 2015 at 09:08 AM 0
Share

couple of things from looking at the code:

cangrab & guigrab seem to be the same, why not use the same variable?

this line:

 if (handsfull == true && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$ouse1) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0))

should probably be else if... because you're setting handsfull = true in the previous if, and then possibly dropping the item.

also, following that, why are you setting handsfull = true when you know it's always true?

avatar image s.stouby · Feb 24, 2015 at 01:04 PM 0
Share
 void FixedUpdate () {
         if (cangrab == true)
         {
             if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse1) && handsfull == false)
             {
                 this.transform.parent = playerH;
                 this.transform.localPosition = new Vector3(0,0,0);
                 this.rigidbody.is$$anonymous$$inematic = true;
                 guigrab = false;
                 handsfull = true;
             }
 
             else if (handsfull == true && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$ouse1) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0))
             {
                 handsfull = false;
                 this.transform.parent = null;
                 this.rigidbody.is$$anonymous$$inematic = false;
             }
         }
     }

You are right about the two variables.. Fool I am.. The handsfull was suppose to be False like this. The problem is that when handsfull is equal to True, you arn't suppose to pick up a new item. But somehow the IF-statement don't care about anything ?

avatar image Owen-Reynolds · Feb 24, 2015 at 04:47 PM 0
Share

It takes practice to do this stuff, especially with ANDs and elses. Realize than in about two months you'll delete this and make a better version from scratch.

If you absolutely have to insult your if statements, suggest that their else's are cheating on them.

avatar image meat5000 ♦ · Feb 24, 2015 at 04:54 PM 0
Share

Using $$anonymous$$et$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown may have left you with an oscillator.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

if Statement Help Increment Value 2 Answers

(if statement) is acting weird 1 Answer

TBG,If,While 1 Answer

How to determine if your List has more than 1 of the same object listed? 1 Answer

Check if gravity is inverted 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