Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnTriggerEnter not working

Discussion in 'Scripting' started by U2-84, May 25, 2020.

  1. U2-84

    U2-84

    Joined:
    Oct 25, 2012
    Posts:
    5
    Hello,
    apparently I am having troubles doing a very simple task.

    What I'm trying to do: instantiate 2 cubes, and if they're "one inside (totally or partially) the other", print "Collision".

    First, I set up the Cube prefab, with a Kinematic Rigidbody attached (and no collider at first), and tagged "Cube".
    Then with the script I instantiate the first cube, scale it (randomly), then add a collider. Once it's done, I instantiate the 2nd cube with the same procedure, and if it's colliding with the first one, the "Collision" message should appear.

    This is the script (attached to a "Script Container" in the scene):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InstantiateCubes : MonoBehaviour
    6. {
    7.     public int count = 2;
    8.     public GameObject spawnOrigin;
    9.  
    10.     public GameObject cubePrefab;
    11.     private GameObject cube;
    12.  
    13.     public float minDistance = 5.0f;
    14.     public float maxDistance = 30.0f;
    15.  
    16.     public float minScale = 0.8f;
    17.     public float maxScale = 1.2f;
    18.  
    19.     private bool collision;
    20.  
    21.     private int i;
    22.     public int seed = 5;
    23.  
    24.     private int collisionsNumber;
    25.    
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         collision = false;
    30.         //Random.InitState(seed); // I activate it in case I want a known seed
    31.         collisionsNumber = 0;
    32.  
    33.         GenerateCollider();
    34.     }
    35.  
    36.     void GenerateCollider()
    37.     {
    38.         for (i = 0; i < count; i++)
    39.         {
    40.             float distance = Random.Range(minDistance, maxDistance);
    41.             float scale = Random.Range(minScale, maxScale);
    42.             cube = Instantiate(cubePrefab, new Vector3(spawnOrigin.transform.position.x + distance, spawnOrigin.transform.position.y + distance, spawnOrigin.transform.position.z + distance), spawnOrigin.transform.rotation);
    43.             cube.transform.localScale = new Vector3(cubePrefab.transform.localScale.x * scale, cubePrefab.transform.localScale.y * scale, cubePrefab.transform.localScale.z * scale);
    44.             BoxCollider cubeCollider = cube.AddComponent<BoxCollider>();
    45.             cubeCollider.isTrigger = true;
    46.         }
    47.     }
    48.  
    49.     private void OnTriggerEnter(Collider other)
    50.     {
    51.         if (other.tag == "Cube")
    52.         {
    53.             collision = true;
    54.             collisionsNumber += 1;
    55.             Debug.Log("Collision! The script generated " + collisionsNumber + " collisions");
    56.         }
    57.  
    58.         else
    59.         {
    60.             collision = false;
    61.         }
    62.     }
    63. }
    So, OnTriggerEnter totally ignores it. I have no feedback on the console, so I assume it's not detecting the collisions.
    I also tried with OnCollisionEnter but it's the same. Also, making the cubes kinematic/non kinematic doesn't have any effect.
    I know I might try the Physics.OverlapBox route but I'd like to solve this problem first.
    Any help? What am I doing wrong?

    Thank you in advance.
     
  2. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    When you add the box collider is it properly stretching to the cube's boundaries?
     
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Is there a script on your cube prefab? The OnTriggerEnter method there is only going to run on the gameobject your InstantiateCubes class is on.
     
    leftshoe18 likes this.
  4. U2-84

    U2-84

    Joined:
    Oct 25, 2012
    Posts:
    5
    Hello,
    thank you guys for your replies.

    Yes, I checked the scene during "play" and colliders have the same exact size of the cubes. I guess it's like when you add a collider to a cube via inspector: it automatically resizes at the size of the cube.

    Damn, this is where I was doing mistakes. The "instantiate script" is attached to a "Script Container" in the scene, and the OnTriggerEnter part should NOT be included in this script, because the Script Container itself doesn't collide with anything, it's just an empty entity that I use to group scene scripts.
    So I created a script that handles the collision part and added it to the cube prefab: now it is correctly sending collision messages.

    Thank you very much.

    Case closed :D
     
    leftshoe18 likes this.