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 Bradwall · Dec 14, 2021 at 02:50 AM · collisiondestroycollision detectiondestroy object

Why are the objects (they are cows) randomly disappearing?

I added this code to some cows in my game so that they would be destroyed when they collide with the outer boundaries in the game, but now they are randomly disappearing at the start. Anybody know why? Heres the code:

 {
     public GameObject edges1, edges2, edges3, edges4, edges5, edges6, edges7;
 
     public void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject == edges1 || edges2 || edges3 || edges4 || edges5 || edges6 || edges7)
         {
             Debug.Log("I am inevitable");
             Destroy(gameObject);
             //add a puff of smoke?
 
         }
     }
 }
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eno-Khaon · Dec 14, 2021 at 03:26 AM

Your if() statement is incomplete. Each portion is a discrete condition, not a "this or this or this" as you're trying to use it. In short, yours functions approximately like this:

 if(collision.gameObject == edges1 || edges2 exists || edges3 exists || ...)



That said, it should be easier to look at (and effectively the same to process) if you turn the 7 GameObjects into an array:

 public GameObject[] edges; // Assign them here or set size/GameObjects in script/etc.
 
 // ...
 
 for(int i = 0; i < edges.Length; i++)
 {
     if(collision.gameObject == edges[i])
     {
         Destroy(gameObject);
         break; // Break out of loop early, since you found a match
     }
 }
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 Bradwall · Dec 15, 2021 at 12:45 AM 0
Share

Thank you for the quick response, I tried the code you wrote and it worked very well. The array was very useful and something I had not thought of. Like I said, the problem is solved, but I am still curious, do you have any idea why only some of the cows were disappearing and others weren't?

avatar image Eno-Khaon Bradwall · Dec 15, 2021 at 05:09 AM 0
Share

Honestly, it's hard to say why there would be any specific (in-)consistency about this. The key element is that it would only be applicable when OnCollisionEnter() is triggered, so maybe there are circumstances where it doesn't immediately/arbitrarily trigger in the first place?


Anyway, a thought:

If these "edges" happen to all be part of the same overall GameObject, you could also simplify this further to not need "edges" at all. If they're all child GameObjects to a shared parent, you could use something like this instead:
 public Transform edgeParent;
 // ...
 if(collision.transform.parent = edgeParent)
 {
     // etc.
 }
avatar image
1

Answer by Zaeran · Dec 14, 2021 at 03:28 AM

You need to specify the == for every object.

i.e.

 if (collision.gameObject == edges1 || collision.gameObject == edges2 || collision.gameObject == edges3, etc.

Your current code only tests for the first edge object on collision, and the rest of the || queries are just querying whether the assigned gameobjects exist.

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

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

206 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Destory() on Collision if Tag = Lazer [SOLVED] 1 Answer

Why can't I destroy this object!? 3 Answers

Help with collisions and destroy please 1 Answer

Collision Detection not working? 1 Answer

Can I have a single rigidbody2d that acts as both collider and trigger? 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