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 /
avatar image
0
Question by Bladiatim · Feb 05, 2019 at 04:06 AM · booleanontriggerstay

OnTriggerStay2D changes bool to true and false at the same time.

Hello. I'm trying to make a 2D multiplayer game on Unity version 2018.3.0f2 Personal where the players have to clean a house before a time limit. This game will involve picking up and dropping items.

I'm using OnTriggerStay2D() and tags to detect whether an item is beneath the player. When the player is over an item, a bool called isInPickUpState should turn true to detect that the player is over an item and can potentially pick it up.

I want to do this because in a situation where the player is not over an item and the player has an item in their inventory, isInPickUpState will turn false and the player drops the item that is in their inventory.

However, when the player goes over an item, the bool turns to false and true at the same time (normally behavior is it only turns to true).

 using UnityEngine;
 
 public class PlayerAction : MonoBehaviour
 {
     bool isInPickUpState = false;
 
     Inventory inventory;
 
     private void Start()
     {
         inventory = GetComponent<Inventory>();
     }
 
     private void OnTriggerStay2D(Collider2D other)
     {
         //Swapping/Picking up objects. Might only work when the player is not using mop/broom
         if (other.gameObject.tag == "Item")
         {
             isInPickUpState = true;
             if (Input.GetKeyDown(KeyCode.X))
             {
                 inventory.PlaceInSlot(other.gameObject.GetComponent<Item>());
             }
         }
     }
 
     private void Update()
     {
         if (isInPickUpState == true)
         {
             if (Input.GetKeyDown(KeyCode.X))
             {
                 Debug.Log("Pickup Running");
             }
         }
         else if (isInPickUpState == false)
         {
             if (Input.GetKeyDown(KeyCode.X))
             {
                 Debug.Log("Normal Running");
             }
         }
     }



Here, I am checking for whether the bool is both true and false at the same time when I press X on top of an item. This is the output.

alt text

I am actually lost on this. I tested this behavior on Unity version 2018.2.11 and it worked fine. Is this a bug or intended behavior?

1234.jpg (14.2 kB)
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
0
Best Answer

Answer by MartinIsla · Feb 05, 2019 at 04:15 AM

Hi! If you're getting both messages at the same time when pressing X, you probably have more than one script in the scene.

Try searching for "t:PlayerAction" in the Hierarchy.

As a sidenote, searching "t:[type]" (e.g t:Light, t:Collider2D) filters all the GameObjects that contain that component. Works in the project window too!

Comment
Add comment · Show 2 · 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 Bladiatim · Feb 05, 2019 at 04:19 AM 0
Share

I have to be the most stupidest person in the world. I do have two of this script in the scene.

Thanks for the help. I would have been lost on the dumbest thing for a very long time.

avatar image MartinIsla Bladiatim · Feb 05, 2019 at 04:22 AM 0
Share

Ha! Don't be so harsh on yourself, this probably happens to everyone. Good luck and keep making games!

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

101 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 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

Bool changing automatically... 2 Answers

Problem with OnTriggerStay2D function 2 Answers

OnTriggerStay not working correctly. 0 Answers

How to detect if a function is "happening"? 1 Answer

Make a boolean positive and then negative with same button 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