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

Destroying a game object on collission with a box collider

Discussion in 'Scripting' started by Hilm, Mar 30, 2008.

  1. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Ok what i'm trying to do is make it check if an object is hitting a box collider I have, and if so delete that object.

    What I have atm is:

    Row1Collider is my box collider.

    And I have this script applied to the game object im trying to destroy.



    Code (csharp):
    1. var IsObjectInRow1 = 0;
    2.  
    3.  
    4. function OnCollisionStay(Row1Collider) {
    5. IsObjectInRow1 = 1;
    6. }
    7.  
    8. function ObCollissionExit(Row1Collider){
    9. IsObjectInRow1 = 0;
    10. }
    11.  
    12. if(IsObjectInRow1 < 0)
    13. {
    14.                     Destroy(gameObject);
    15. }
    This isn't working, the objects remain showing. Can anyone tell me where i'm going wrong plz?

    Thanks!
    Adam.
     
  2. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    well first you can change < to > now it is saying:
    if isObjectInRow is less then zero destroy the gameObject.

    But why not do like this


    Code (csharp):
    1.  
    2. function OnCollisionStay(Row1Collider) {
    3.       Destroy (gameObject);
    4. }
    5.  
     
  3. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Basically it's a tectris like game but in 3d.

    There are box colliders where each segment could land.

    i'm basically trying to do:

    if all colliders for row 1 are triggered then destroy all objects on row1.

    I'm sorting out which objects are on row one using a box collider.

    Lol cant believe I didn't see the < >, so dumb of me.

    I'll try it out, hopefully it'll work :).

    Thanks!
    Adam
     
  4. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Ok still got a problem...

    Heres all the code im using so far (if you know a better way of doing all the if's then let me know :S)

    Code (csharp):
    1. static var CongratulationsMessage;
    2. static var ParticleEffect;
    3. private var IsObjectInRow1 = 0;
    4.  
    5.  
    6. function OnCollisionStay(Row1Collider) {
    7. IsObjectInRow1 = 1;
    8. }
    9.  
    10. function ObCollissionExit(Row1Collider){
    11. IsObjectInRow1 = 0;
    12. }
    13.  
    14. function Update()
    15. {
    16. Level1Collission();
    17. }
    18.  
    19. function Level1Collission()
    20. {
    21.    
    22.    
    23.    
    24.     if(Collider1.Collission > 0)
    25. {
    26.     if(Collider2.Collission > 0)
    27. {
    28.     if(Collider3.Collission > 0)
    29. {
    30.     if(Collider4.Collission > 0)
    31. {
    32.     if(Collider5.Collission > 0)
    33. {
    34.     if(Collider6.Collission > 0)
    35. {
    36.     if(Collider7.Collission > 0)
    37. {
    38.     if(Collider8.Collission > 0)
    39. {
    40.     if(Collider9.Collission > 0)
    41. {
    42.     if(Collider10.Collission > 0)
    43. {
    44.     if(Collider11.Collission > 0)
    45. {
    46.     if(Collider12.Collission > 0)
    47. {
    48.     if(Collider13.Collission > 0)
    49. {
    50.     if(Collider14.Collission > 0)
    51. {
    52.     if(Collider15.Collission > 0)
    53. {
    54.     if(Collider16.Collission > 0)
    55. {
    56.     if(Collider17.Collission > 0)
    57. {
    58.     if(Collider18.Collission > 0)
    59. {
    60.     if(Collider19.Collission > 0)
    61. {
    62.     if(Collider20.Collission > 0)
    63. {
    64.     if(Collider21.Collission > 0)
    65. {
    66.     if(Collider22.Collission > 0)
    67. {
    68.     if(Collider23.Collission > 0)
    69. {
    70.     if(Collider24.Collission > 0)
    71. {
    72.     if(Collider25.Collission > 0)
    73. {
    74.     if(Collider26.Collission > 0)
    75. {
    76.     if(Collider27.Collission > 0)
    77. {
    78.     if(Collider28.Collission > 0)
    79. {
    80.     if(Collider29.Collission > 0)
    81. {
    82.     if(Collider30.Collission > 0)
    83. {
    84.             yield new WaitForSeconds (1.5);
    85.             ParticleEffect = 1;
    86.             Debug.Log("All Hit");
    87.             CongratulationsMessage = "Well done you completed a full row!";
    88.             yield new WaitForSeconds (5);
    89.            
    90.             if(IsObjectInRow1 > 0)
    91.                 {
    92.                     Destroy(gameObject);
    93.                 }
    94.  
    95.  
    96.             ParticleEffect = 0;
    97.             CongratulationsMessage = "";
    98.  
    99. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
    100.  
    101.  
    102. }
    103.  

    Now basically its like tectris kind of. I have this script applied to each block that falls.

    For some reason when a "row" is completed it deletes all the blocks, (for example if I begin stacking the objects, as soon as I complete row 1, all the objects dissapear. Not just those on row 1.

    Anyone know where Im going wrong?

    Ps I know this code is probarbally horriffic and a insane way of doing it. Please feel free to let me know how to better do it. It was just the first way that popped into my head and as im still learning...


    Thanks!
     
  5. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    try with a loop (search for "while" in the script reference)
     
  6. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    While has 35 instances in the Scripting Refrence so errrr how do I go about doing what ure suggesting? (sorry i'm really new to this :S)
     
  7. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    hm it does not seam to be in the scripting ref.
    well i can give you an example.


    Code (csharp):
    1.  
    2. var number = 8;
    3. var something = 0;
    4.  
    5. function Update () {
    6.         while (something < 8) {
    7.                 print (something);
    8.                 something += 1;
    9.         }
    10. }
    this will print
    0
    1
    2
    3
    4
    5
    6
    7
    8
    every frame because it runs the code 9 times.
     
  8. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Ok sorry how would I go about using this to destroy the row?

    Like:

    while (IsObjectHittingRow1 >0){
    Destroy(gameObject);
    }

    Sorry If i'm being dumb. I was up till 6 am working on this and learning and stuff so my brain is like not functioning so well atm :p.
     
  9. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    mm what type is collider1 (for example) gameObject collider etc
     
  10. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    oh and do you know exactly how many objects that is required for one row?
     
  11. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
  12. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Ok collider 1 is the first of the box colliders,

    Currentally theres 30 colliders per row, but as the player progresses I expect this to increase.

    basically i'm trying to just say if all collides are active then delete all objects currentally colliding with Row1Collider.


    Each of the 30 colliders in row 1 has it's own script. The scipt Ive posted is then applied to each falling object. It checks if all the 30 colliders is active and then what I need help on it how to make it simply destroy the objects colliding with a collider that covers all of row 1.

    Any boxes stacked on row 2 should then fall to row 1 but not be deleted as the script should loop, the colliders should then all not be "active" and the object origionally on row 2 shouldn't be destroyed.

    My current problem is when row one is complete it does destroy objects which are stacked and i'm not sure why :S.
     
  13. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Ok her comes the SOLUTION :D :!: :!:

    place this on your colliders

    Code (csharp):
    1. private var row1 : GameObject;
    2.  
    3. function OnCollisionEnter () {
    4.     row1.GetComponent (mainscript).active_colliders += 1;//you may have to set "" marks on mainscript
    5. }
    6. function Start () {
    7.     row1 = GameObject.Find ("row1");
    8. }
    and this in a gameObject called row1 and the script must be named mainscript
    Code (csharp):
    1.  
    2. private var active_colliders = 0;
    3. var row_colliders = 30;
    4. var particle : GameObject;
    5.  
    6. function Update () {
    7.     if (active_colliders == row_colliders) { // if all the colliders are triggered
    8.         var hits : RaycastHit[];
    9.         //raycast through all the objects in the row
    10.         RaycastAll (transform.position,gameObject.transform.TransformDirection (Vector3.forward),100);
    11.         for (var i = 0;<hits.length;i++) { // this is the loop (i thought it was "while" but it was "for"
    12.             var hit : RaycastHit = hits[i]
    13.             Destroy (hit.transform.gameObject);
    14.             Instantiate (particle, hit.transform.position, Quaternion.identity); // make some particle effects (one per object)
    15.         }
    16.     }
    17. }
    the object row1 must be placed like this:
     

    Attached Files:

  14. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Sorry I think you think i'm making 2d tectris in 3d. Where as I'm actually making something like tectris in 3D.

    Meaning the "box" has 3 dimensions.

    Im sooo confused lol. :S

    I'm gonna take a break and come back to it :S
     
  15. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    like this then
     

    Attached Files:

  16. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    like this then
     

    Attached Files:

  17. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Ok I think I can sort it.

    Only thing I need to know is, is there a way to have a function of a run time function?

    Essentially:

    Code (csharp):
    1. function MyFunction()
    2. {
    3.    var GameObjectHitRow1Collider = 0;
    4.  
    5.       function OnCollissionStay(Row1Collider)
    6.       {
    7.           GameObjectHitRow1Collider = 1;
    8.       }
    9.  
    10.       if (GameObjectHitRow1Collider > 0)
    11.       {
    12.          Destroy(gameObject);
    13.          GameObjectHitRow1Collider = 0;
    14.       }
    15.  
    16. }
     
  18. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    like this then

    Oh dubble post
     

    Attached Files:

  19. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    run time ?

    oh by the way in your script you are deleting the gameObject the script is attached to
     
  20. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    yeah thats the idea. I attach it to the colliders, it should check if the object is colliding with row 1 and then should delete itself.

    There would also be a bunch of if statments before to check all row1 colliders would be working but...
     
  21. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
  22. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Nah basically is there a way to do functions within functions?
     
  23. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    mm no i don't think so...