Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. The Burst compiler has its own forum section now.
    Dismiss Notice
  4. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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.
     
unityunity