x


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.

more ▼

asked Dec 07 '10 at 01:46 PM

Taalar gravatar image

Taalar
13 1 1 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Dec 07 '10 at 02:16 PM

bjarnefisker gravatar image

bjarnefisker
237 1 2 12

collision.gameObject, not collision.gameobject.

Dec 07 '10 at 02:19 PM Statement ♦♦

fixed :) thanks

Dec 07 '10 at 02:22 PM bjarnefisker

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

Dec 07 '10 at 02:40 PM Proclyon

Thanks alot! It works like a charm. I am new to unity (started using it last week) and new to C# (programming 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.

Dec 07 '10 at 03:21 PM Taalar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x1250
x764
x202

asked: Dec 07 '10 at 01:46 PM

Seen: 2130 times

Last Updated: Dec 07 '10 at 01:53 PM