Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My current scripts creates and modifys the location of various prefab objects as they go along the screen. One of the things I want to find out about the active object is whether it has collided with another object, and if so, to destroy itself. The problem is that every example I've found to date has no way to access the object itself to get this information.

        if (Random.value > 0.9)
    {
        var bomberInstance = GameObject.Find("bomber");

        var newBombObject = Instantiate(Resources.Load("_prefabs/bomb"), new Vector3(bomberInstance.transform.position.x, bomberInstance.transform.position.y, bomberInstance.transform.position.z), Quaternion.identity) as GameObject;


        bombArray.Add(newBombObject);
    }

    foreach (GameObject bomb in bombArray)
    {

        //get bomb objects collision status here.

        var newBombYAxisPosition = bomb.transform.position.y - .1f;

        bomb.transform.position = new Vector3(bomb.transform.position.x, newBombYAxisPosition, bomb.transform.position.z);

    }

As you can see, the examples you find online don't exactly fit very well for what I need. Do objects in unity store this information, and if so how can it be accessed? And if not, is there another way to access this information?

share|improve this question
    
So you want info for every object either it is already collided with any other object? Or something like to destroy current object on collision? Make it more clear please – Hamza Hasan Feb 11 at 6:15

If I get you right, one way would be to add a script to your Bomb Prefab that implements the OnTriggerEnter and OnTriggerExit method.
There should also be a collider attached, the collider should be marked as trigger. If it is a mesh collider, make sure it is also convex, otherwise the OnTriggerEnter/Exit methods will not be called.
In OnTriggerEnter/Exit you should count the collisions:

BombScript.cs (attached to the bomb GameObject):

int collisions = 0;
public bool HasCollisions {
  get { return collisions > 0; }
}

void OnTriggerEnter(Collider other) {
  //Check other collider here. Checks can be like type, or layer. If it is the check you want, increment collisions.
  collisions++;
}

void OnTriggerEnter(Collider other) {
  //Check other collider here. Checks can be like type, or layer If it is the check you want, decrement collisions.
  collisions--;
}

You then can get the script of the Bomb and check the Collisions property. If it is true, delete the Bomb and do whatever you want: foreach (GameObject bomb in bombArray) {

    //get bomb objects collision status here.
    BombScript bs = bomb.GetComponent<BombScript>();
    if(bs.HasCollisions)
    {
        //Do stuff here...
    }
    var newBombYAxisPosition = bomb.transform.position.y - .1f;

Since you modify the position of the bomb via the transform, you might also need a Rigidbody component which is set to isKinematic. You can find more information about colliders here: http://docs.unity3d.com/Manual/CollidersOverview.html

share|improve this answer
    
Should all of this code be in one file? Because none of these functions fire when hitting other colliders, even when tigger is set to true. – canadiancreed Feb 11 at 23:52
    
Is there any particular reason you've suggested that the OP has to use triggers instead of normal colliders? – Jack Feb 12 at 0:51
    
@Jack: It's based on the way the Bomb is moved. Since he manipulated the transform.position directly. Maybe a Rigidbody with isKinematic is necessary too. – Skalli Feb 12 at 8:47
    
@canadiancreed: Maybe a rigidbody is needed then. There are many cases where Unity does not fire the events. When you move a GameObject by manipulating the transform component directly the colliders might not be updated. I'll modify my answer a bit to reflect these circumstances. – Skalli Feb 12 at 8:49

Assuming you have a collider on each object involved in the collision, access the other GameObject through the collider. This is what I typically do:

void OnCollisionEnter(Collision collision) {
    MyOtherScript other = collision.collider.GetComponent<MyScript>();

    //We know it is the right kind of object by whether is
    //has a certain script attached
    if(other && other.CheckSomeCondition()) {
        Destroy(gameObject); //self-destruct
    }
}

This is assuming that the object your checking for has a script called MyOtherScript attached, containing a method called CheckSomeCondition().

share|improve this answer
    
Returns error "The name Destroy does not exist in the current context. Also the function is never accessed – canadiancreed Feb 12 at 0:11
1  
If the script inherits from MonoBehaviour, this shouldn't be a problem. There's likely some other issue at play, depending on whether you literally copy pasted the answer or did some interpretation of your own. This answer as it stands isn't going to solve your problem without tweaking. – Jack Feb 12 at 0:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.