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. Dismiss Notice

Bug OnTriggerEnter not running

Discussion in 'Scripting' started by WayldWest, Jun 16, 2023.

  1. WayldWest

    WayldWest

    Joined:
    May 11, 2023
    Posts:
    3
    Hello! I have a problem with collision detection.

    The first script is attached to an empty game object in my scene. In that script I am creating an empty game object and adding 2 components to it: one sphere collider and a c# script stored in the assets folder (2nd script below). The first script also instantiates a game object which in this case is a regular cube created from the hierarchy and stored as a prefab.

    Before I run the game I get a warning message: Assets\Scripts\CollisionDetector.cs(7,10): warning CS0414: The field 'CollisionDetector.isColliding' is assigned but its value is never used.

    When I run the game, the 2 game objects which I create from the first script appear in the hierarchy. I get the Debug.Log message: "Settings made" from the second script. Then nothing happens. I get no error messages and no Debug.Log message: "Collision detected".

    What am I doing wrong in this code and what do I have to fix to make the collision work?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawningGameObject : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] GameObject cubePrefab;
    9.  
    10.     void Start()
    11.     {
    12.         GameObject collisionDetector;
    13.  
    14.         collisionDetector = new GameObject("Collision Detector");
    15.         collisionDetector.transform.position = new Vector3(0f, 0f, 0f);
    16.         collisionDetector.AddComponent<SphereCollider>();
    17.         collisionDetector.AddComponent<CollisionDetector>();
    18.  
    19.         Instantiate(cubePrefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
    20.     }
    21.    
    22. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CollisionDetector : MonoBehaviour
    6. {
    7.     bool isColliding = false;
    8.     SphereCollider sphereCollider;
    9.  
    10.     void Awake()
    11.     {
    12.         sphereCollider = gameObject.GetComponent<SphereCollider>();
    13.         sphereCollider.radius = 0.1f;
    14.         sphereCollider.isTrigger = true;
    15.         Debug.Log("Settings made");
    16.     }
    17.  
    18.     void OnTriggerEnter(Collider other)
    19.     {
    20.         isColliding = true;
    21.         Debug.Log("Collision detected");
    22.     }
    23. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    OnTriggerEnter / OnCollisionEnter and other physics callbacks:

    Best place to start is with the online Manual, which explicitly lists the requirements to receive physics interaction callbacks.

    If you still can't get it, make a blank scene, a test script and work with the Manual and Scripting Reference example code until it works. We know it works, so don't stop until you have the small scene example working, then go back to your main game scene.
     
  3. WayldWest

    WayldWest

    Joined:
    May 11, 2023
    Posts:
    3
    Thank you for helping. This is too complicated and takes too much time. I will have to find another solution for my game.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Like the documentation says the trigger must have a Rigidbody component.

    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    I understand you're just getting started and don't have the experience to know but this is a trivially simple problem and the complexity of problems will only continue to increase so you need to get into the mindset that you have to be willing to put time into solving them.

    You can only go so far with questions on a forum. At some point you need to be able to do proper troubleshooting.
     
    Last edited: Jun 25, 2023
    Chubzdoomer likes this.