Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make an object explode on collision?

Discussion in 'Scripting' started by herbie, Sep 30, 2013.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Just as in this old topic http://answers.unity3d.com/questions/140468/how-to-make-an-object-explode.html,
    I have downloaded the explosion framework and want to have the explosion on collision.

    I have a ball and when this ball hits anything, it must be explode.
    I have attached the detonator script and the script below to the ball.

    Code (csharp):
    1. #pragma strict
    2.  
    3. public var ball: GameObject;
    4.  
    5. function OnCollisionEnter()
    6. {
    7.     ball.GetComponent("Detonator").Explode();
    8. }
    Now I have the error "'Explode' is not a member of 'UnityEngine.Component'"

    Does that something to do with the fact that my script is javascript and the detonator script is c## ?

    (I'm struggling with it for a couple of hours now and I think I'm overlooking something stupid).
     
  2. Toerktumlare

    Toerktumlare

    Joined:
    Sep 15, 2013
    Posts:
    74
    overlooking yes... stupid? well that could be discussed :) no im sorry that was not nice of me

    if you check the unity script reference for the usage of OnCollisionEnter() you must in the a collision object into the function. Then from this collisionobject you can extract the information of what objects has collided and then what to do with it.

    Code (csharp):
    1.  
    2. function OnCollisionEnter(collider : Collider){
    3.    
    4.     //if the collided object is the object with a name of ball
    5.     if(collider.GameObject.name == "ball"){
    6.        
    7.         //then get the component detonate and call the function explode
    8.         collider.gameObject.getComponent("Detonator").Explode();
    9.     }
    10. }
    OnCollisionEnter

    http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks for reply.

    It still says that 'Explode' is not a member of 'UnityEngine.Component.

    No problem to call me stupid because I think I did this before :oops: but I lost it.

    Is it right that line 4 in your code must be:
    Code (csharp):
    1.  if(collider.gameObject.name == "ball"){
    instead of
    Code (csharp):
    1.  if(collider.GameObject.name == "ball"){
     
    Last edited: Sep 30, 2013
  4. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    bad: collider.GameObject.name
    good: collider.gameObject.name

    GameObject is a class
    gameObject is an instance

    ..back to school :)
     
  5. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Could it be that the detonator script is not accessible for some kind of reason?
     
  6. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Attach a empty GO to your object and instantiate a explosion/particle system from it.