Search Unity

code won't work

Discussion in 'Scripting' started by Hard target, Aug 28, 2014.

  1. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    This code does not work even with a box collider set to is trigger.What would be causing this?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyCubes : MonoBehaviour
    5. {
    6.     void OnCollisionEnter (Collision col)
    7.     {
    8.         if(col.gameObject.name == "prop_powerCube")
    9.         {
    10.             Destroy(col.gameObject);
    11.         }
    12.     }
    13. }
     
  2. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    What is this script attached to? How do you have it set up?
     
  3. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    This script is attached to a cube with a box collider set to is trigger.A dynamic cube hits this cube and it should be destroyed.But nothing happens.
     
  4. Beennn

    Beennn

    Joined:
    Sep 11, 2010
    Posts:
    373
    Use OnTriggerEnter instead.
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Triggers would call OnTriggerEnter, not OnCollisionEnter. Deselect "Is Trigger" if you want to use OnCollisionEnter.
     
  6. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    That did not work.How can I post my project?
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Does one of your objects have a non-kinematic Rigidbody?
     
  8. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Make sure "Is Trigger" is not selected...sometimes name can get a bit tricky, use gameObject.tag instead.
     
  9. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    It maybe does?
     
  10. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    The colored cubes that fall had to have the script on them. while the cubes on the ground had to have the tag.Both cubes had to have rigid bodies and colliders not set to is trigger.I had it all mixed up.But i had not tried it with game object.name yet.
     
  11. Andrew Chami

    Andrew Chami

    Joined:
    Oct 27, 2012
    Posts:
    22
    void OnTriggerEnter(Collider col){} is what you should be using.