Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Bingowaders · Jan 14 at 09:52 PM · destroyif-statementsontriggerstaydestroygameobject

Is Destroy(transform.parent.gameObject) Unreliable?

Good day. On my first project here.

Im trying have it so when the Player gets in range of the "Powerup" GameObject, a UI text will tell them to "press 'E'" to pick up. When they do so their JumpHeight is affected and the Powerup Object & text are destroyed.

However I found that eventhough the JumpHeight stats are always changed, the GameObject & UI aren't destroyed every time, usually requiring to press 'E' 2 to 4 times for it to work. I can't seem to figure out what the issue is.

This is split on two scripts:

Script below is the first one, set to the Collider around the "Powerup" GameObject, Parent of said Collider

 public class JumpAlert : MonoBehaviour
 {
     public GameObject uiText;
     void Start()
     {
         uiText.SetActive(false);
     }
     public void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             uiText.SetActive(true);
             
         }
         
     }
     public void OnTriggerStay(Collider other)
     {
 
         if (other.gameObject.tag == "Player" && Input.GetKeyDown("e"))
         {
             uiThing.SetActive(false);
             Destroy(transform.parent.gameObject);
         }
 
     }
        public void OnTriggerExit(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             uiThing.SetActive(false);
         }
     }
    
 }


Script below are the relevant parts of the second script, set to the Player Controller.

 public Transform powerCheck;
 public float powerCheckArea = 1f;
 public LayerMask powerCheckMask;

-

  nearpowerup = Physics.CheckSphere(powerCheck.position, powerCheckArea, powerCheckMask);
 
 
         if(nearpowerup && Input.GetKeyDown("e"))
         {
             jumpHeight = 100f;
             gravity = -7;
             Debug.Log("POWERS AQUIRED");
 
 
         }

As you can see the same If statement Input requirement is set in both scripts, which I could asume creates problems when two scripts are trying to use at the same time? You probably can tell im a total beginner.

To note is that if i change Destroy(transform.parent.gameObject) to Destroy(gameObject) to kill the Collider itself, it works fine, hence the title question.

Not sure how relevant this might be, but it seems to work if I enter the Collider, exit it, and then enter it again.

Thank you.

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
2
Best Answer

Answer by Chubzdoomer · Jan 14 at 11:18 PM

According to the documentation, OnTriggerStay isn't guaranteed to run every frame. I'm assuming that's the reason your key presses aren't always being registered.

There are a few different ways you can solve this. One of the easiest is to use a boolean variable that's set to True in OnTriggerEnter and False in OnTriggerExit (or vice versa). You can then handle your input inside Update (which is guaranteed to run every frame) and react differently according to whether the boolean is True or False. As for OnTriggerStay, you can just ditch it completely.

Here's some revised code you can try out:

  public class JumpAlert : MonoBehaviour
  {
       public GameObject uiText;
       bool playerIsColliding = false;
       
       void Start()
       {
           uiText.SetActive(false);
       }
       
       void Update()
       {
           if (Input.GetKeyDown("e") && playerIsColliding)
           {
               Destroy(transform.parent.gameObject);
           }
       }
       
       void OnTriggerEnter(Collider other)
       {
           if (other.gameObject.tag == "Player")
           {
               uiText.SetActive(true);
               playerIsColliding = true;
           }
       }
       
       void OnTriggerExit(Collider other)
       {
           if (other.gameObject.tag == "Player")
           {
               uiThing.SetActive(false);
               playerIsColliding = false;
           }
       }
  }

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 Bingowaders · Jan 14 at 11:43 PM 0
Share

It seems to work! Thank you!

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

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

Destroy 2D Objects when in contact 0 Answers

Destorying gameobjects on a network 0 Answers

Why are my Game Objects being destroyed? 0 Answers

How to use a local var in the script 2 Answers

Destroy character controller and instantiate new one 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