Search Unity

void OnTriggerEnter(Collider other) Dont work properly.

Discussion in 'Scripting' started by cruising, Oct 16, 2019.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    I have some problem that i didn't have before.
    I have a pretty much empty space scene, a root gameobject that have the spaceship movement script (rigidbody), the root object also have a child with the GridName script on it and a trigger collider, inside that child i have the actual spaceship model

    The root object also have the spaceship UI and Maincamera so everything is like a package that you can transfer between scenes.

    In the scene i have 6 cubes 3x3 with only the colliders left and set to Trigger.
    In the GridName script is everything done so that the collider you hit will be sent to the UIs text field so you know what square you are in on the map, pretty simple.

    How ever, sometimes the collision is not detected even if you fly through 3-4 cubes , sometimes it get detected pretty much in the center of the cube, it should be detected as soon the ships trigger collider collide with the cubes like it did perfect before. All layers is properly set up to be able to collide with each other.

    Here is the GridName code.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class GridName : MonoBehaviour
    8. {
    9.     public Text SectorName;
    10.  
    11.     void OnTriggerEnter(Collider other)
    12.     {
    13.         //Debug.Log("Sector Unit", gameObject);
    14.         if (other.gameObject.CompareTag("Sector"))
    15.         {
    16.             SectorName.text = other.name.ToString();
    17.         }
    18.     }
    19. }
    20.  
    21.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I've had similar issues until I realized that the collisions happened in child objects, and I needed to compare with root (I think I removed the RB at some Point)
     
    Boodums likes this.
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    The collison is suposed to happen in the child object where the script and the collider is located.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I see you already log (or rather: did log :) ) the collision detection. Did the detection happen (i.e. is the tag at fault)?
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
  6. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Yes i did log before :)
    I added it back and it did only log the first collision for the first collision where the ship is when scene starts.
    And the layers is setup correct, this script did work just a couple of days ago perfectly, and now it is totally broken.