Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to set collisions on script created objects

Discussion in 'Physics' started by Alco6, Jul 16, 2019.

  1. Alco6

    Alco6

    Joined:
    Oct 19, 2018
    Posts:
    13
    Hello!

    I would like to detect collisions on objects created from script.
    Is it possible to create an object and to set a Collision behavior (OnCollisionEnter) to the object created directly by script?

    This does not work:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Button : MonoBehaviour {
    6.  
    7.     BoxCollider collider1;
    8.  
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.  
    15.         //Button LEFT
    16.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    17.        
    18.         cube.transform.position = new Vector3(20, -30, 220);
    19.         cube.transform.localScale = new Vector3(20, 20, 5);
    20.         Rigidbody cuberigidbody = cube.AddComponent<Rigidbody>();
    21.         cuberigidbody.useGravity = false;
    22.         cuberigidbody.isKinematic = true;
    23.  
    24.         collider1 = cube.AddComponent<BoxCollider>();
    25.         collider1.isTrigger = true;
    26.        
    27.        
    28.  
    29.     }
    30.  
    31.  
    32.      void OnCollisionEnter(Collision collision)
    33.     {
    34.         UnityEngine.Debug.Log("S");
    35.     }
    36.  
    37.  
    38.     // Update is called once per frame
    39.     void Update () {
    40.        
    41.     }
    42. }
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,529
    You need to add an script with OnCollisionEnter to your recently created component. For example:
    • Move the OnCollisionEnter method of your example to an independent script named "MyCollisionDetector".
    • Add this line to the code where you create the object:
      cube.AddComponent<MyCollisionDetector>();
    Now the OnCollisionEnter method in MyCollisionDetector will be invoked whenever the newly created cube detects a collision.

    However, as for your code, you should have other things in mind as well:
    • The primitive cube already includes a collider, which will be the responsible of detecting the collisions. You're also adding a trigger, but this one won't raise collisions. Use OnTriggerEnter instead for detecting the interactions with the trigger.
    • As your cube is kinematic, it won't raise collision events unless you configure the parameter at Project Settings > Physics > Contact Pairs Mode. I'd configure it to "Enable All Contact Pairs" to ensure it always work as expected.
    • The correct method to move a kinematic rigidbody is calling rigidbody.MovePosition and rigidbody.MoveRotation every FixedUpdate.
    Hope this helps!