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 Nicolas64pa · Jul 13, 2021 at 07:40 PM · destroytoggle

How can i make it so that when a gameobject enters a collider, it gets destroyed. All of this while being able to toggle said collider by pressing a key

So i have been trying for the past few days how to do as I say in the title but i havent made much progress, if at all

What i have done is, I created an empty object and added a box collider to it, then i wrote this code on the player's script to make the empty toggleable, which hasnt worked as it only sets it to false when the empty is set to true.

  if (Input.GetKeyDown(KeyCode.E) && toggleAttack)
         {
             attack.SetActive(true);
             toggleAttack = false;
         }
         if (Input.GetKeyDown(KeyCode.E) && toggleAttack == false)
         {
             attack.SetActive(false);
             toggleAttack = true;
         }

Then on the empty's script i wrote this so that it would destroy a certain type of gameobjects and also follow the player

 {
     public GameObject player;
     public GameObject attack;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.position = player.transform.position + new Vector3(-0.8f, 0, -1);
     }
     private void OnTriggerEnter(Collider other)
     {
         if (CompareTag("Enemy"))
         {
             Destroy(other.gameObject);
             GetComponent<SpawnManager>().currentEnemies -= 1;
         }
     }
 }

Which also doesnt work as the empty just ignores any of the gameobjects that enter its trigger

Any help would be appreciated

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

Answer by starviw · Jul 14, 2021 at 08:02 AM

hey, first thing first, you can write the player's code as below just to simplify it

 if (Input.GetKeyDown(KeyCode.E))
 {
     attack.SetActive(toggleAttack);
     toggleAttack = !toggleAttack;
 }


secondly, you should compare other.gameobject.tag to compare the tag of the object you collided with and not just CompareTag, which i'm not sure what it does since it's a function by itself.


thirdly, you need to check that at least one of the objects, be it empty or the enemy, has rigidbody on it for collisions to occur.


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 Nicolas64pa · Jul 14, 2021 at 10:00 AM 0
Share

Hey there, tried all 3 of the things you said and worked out perfectly as i wanted it to, thank you very much and have a great day.

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

125 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

Related Questions

spawn and destroy with toggle button? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

My player wont get destroyed when he dies ,My player wont get destroyed when he dies 1 Answer

How to destroy gameobject in hierarchy with a bool list. 2 Answers

A* Pathfinding How to destroy seeker,A* Pathfinding What command to destroy seeker 0 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