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
1
Question by Taalar · Dec 07, 2010 at 01:46 PM · collisionprefabdestroyinstance

How to refer to a collision between clones of the same prefab? C#

Hi, I am building a game in 2D (or 2.5D if you want) and it only consists of one screen where the player (a sphere) can move around, and other spheres are spawned and moving from outside the screen against the middle. these other spheres (called enemies at the moment, but aren't actually enemies) are supposed to destroy themselves ehwn colliding with other enemies.

My problem is that I can't get them to know when colliding with an other enemy. The nearest I have come in many attempts is this, which is written in the enemy spawner:

    if (Collision.Equals(GameObject.FindGameObjectWithTag("enemy"), GameObject.FindGameObjectWithTag("enemy")))
    {
        Destroy(GameObject.FindGameObjectWithTag("enemy"));
    }

Which will make it so that it "collides" with it self and selfdestruct on spawn.

I have also tried to make a method in the enemy script that kills( Destroy(gameObject) ) it, which was called from the enemy spawner script, but then unity said I couldn't destroy in risk of data loss, which means it thought I wanted to destroy the prefab, not the clone. This is weird as I have stated the same thing in update if the clone is too far from the 0.0 coordinates and there it works great.

Well, thankful for help,

Robin

EDIT: I forgot to say (except in the title) that I use C# with monodevelopment in visual studio.

Comment
Add comment · Show 1
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 Waleed Khan · Aug 12, 2015 at 11:38 AM 0
Share

i m doing the same but both collided destroyed tell me the solution for it

1 Reply

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

Answer by bjarnefisker · Dec 07, 2010 at 02:16 PM

Why not put a script on each enemy that uses OnCollisionEnter and test for what the object has collided with? About the OnCollisionEnter:

OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.

So it should not "collide" with itself.

void OnCollisionEnter(Collision collision) {
   if(collision.gameObject.tag == "enemy")
      Destroy(gameObject);
}

The script above would delete the enemy when it collides with another enemy if it is placed in a script attached to the enemies.

Hope this helps

Comment
Add comment · Show 4 · 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 Statement · Dec 07, 2010 at 02:19 PM 1
Share

collision.gameObject, not collision.gameobject.

avatar image bjarnefisker · Dec 07, 2010 at 02:22 PM 0
Share

fixed :) thanks

avatar image Proclyon · Dec 07, 2010 at 02:40 PM 0
Share

Good solution, typical C# approach and very basic/simple/not complicated for anyone. +1

avatar image Taalar · Dec 07, 2010 at 03:21 PM 0
Share

Thanks alot! It works like a charm. I am new to unity (started using it last week) and new to C# (program$$anonymous$$g over all..) which I started with for about a month ago, so I guess I am a bit unsecure with Unity specific functions and such still.

Again, thanks a lot, and hope this will help others as well.

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

1 Person is following this question.

avatar image

Related Questions

Destroy an instance of a prefab 1 Answer

Destroying a prefab on collision with a cube? 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

Collision between two moving clones 0 Answers

Can prefabs interact with scroll rect? 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