Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jul 12, 2016 at 04:30 PM by Spelix95 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Spelix95 · Jul 11, 2016 at 03:51 PM · collisionbooleanlayers

Boolean sets itself to false when using layers.

I have a script that will pick up an item when pressing "Pickup" button which is K. If you're already holding an item it will drop it instead.

It was working perectly fine until I placed the item to pickup and the trigger that detects the items into other layers, so that the trigger can only collide with the items and items can't collide with the player collider.

Now whenever I try to pickup an item, it will pick it up then immediately drop it. It seems the holdPickup bool will reset to false when it shouldn't causing if (Input.GetButton ("Pickup") && !holdPickup && charInventory.isHoldingItem ()) { to be true, which will call the drop method from another script.

holdPickup is to check if the player has let go of the button and pressed it again so that this wont happen.

Even if I remove if (!Input.GetButton ("Pickup") && holdPickup) { holdPickup = false; } from the script so that there is nothing nowhere that sets holdPickup back to false, it will still be set back to false. There is nothing in any of my other scripts that sets holdPickup to false.

Any idea what would be causing this? I have'nt worked with layers before, what would make layers cause a bool to set back to false? It seems ilogical...

Thanks for any answer!

 using UnityEngine;
 using System.Collections;
 
 public class CharPickUp : MonoBehaviour {
     CharInventory charInventory;
     bool holdPickup;
 
     void Start() {
         charInventory = transform.parent.GetComponent<CharInventory> ();
     }
 
     void Update() {
         if (!Input.GetButton ("Pickup") && holdPickup) {
             Debug.Log ("Let go of Pickup button");
             holdPickup = false;
         }
         if (Input.GetButton ("Pickup") && !holdPickup && charInventory.isHoldingItem ()) { //calls on drop method.
             Debug.Log ("Call on drop.\nButton = " + Input.GetButton ("Pickup") + ". holdPickup = " + holdPickup + ". isholding = " + charInventory.isHoldingItem());
             holdPickup = true;
             charInventory.getHoldingItem ().GetComponent<PickUpableItem> ().Dropped ();
             charInventory.setHoldingItem (null);
         }
     }
         
     void OnTriggerStay2D(Collider2D col) {
         if (Input.GetButton ("Pickup") && !holdPickup && !charInventory.isHoldingItem ()) {
             holdPickup = true;
             Debug.Log ("Tried to pick up " + col.gameObject);
             Debug.Log ("holdPickup = " + holdPickup);
             switch (col.gameObject.tag) {
 
             case "rock":
                 charInventory.setHoldingItem (col.gameObject);
                 col.gameObject.GetComponent<PickUpableItem> ().PickedUp (this.gameObject);
                 break;
 
             case "branch":
                 charInventory.setHoldingItem (col.gameObject);
                 col.gameObject.GetComponent<PickUpableItem> ().PickedUp (this.gameObject);
                 break;
             }
         }
     }
 }



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

  • Sort: 
avatar image
0
Best Answer

Answer by Spelix95 · Jul 12, 2016 at 04:30 PM

A few hours after posting this question, I randomly stumbled on the answer.

This script is dublicated from an older one that only handled both punching and picking up. We decided it was better to split the two of them up and so we did. The problem then appeared.

The thing was that we never removed the earlier script from the trigger, only applied the new ones. So each time we tried to pick something up, we had two seperate pick-up scripts running, one picked it up and held it, the other dropped it immidatly.

TL;DR: Don't dublicate scripts and forgetting to remove the old one

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

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where collision matrix in unity3d 5+ ? 0 Answers

Timer onTriggerEnter doesn't launch 0 Answers

Stop Particle System From Colliding With Itself 0 Answers

How can I make my script recognize my player is in some layer(Thats not solid)? 0 Answers

I'm trying to make the collision work. But... 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