Search Unity

Question Cannot Trigger my Callout Menu

Discussion in 'Scripting' started by coysterry, Feb 27, 2024.

  1. coysterry

    coysterry

    Joined:
    Jan 14, 2024
    Posts:
    5
    Hi, Unity newbie here. I wrote a script to trigger a callout menu I added to my game. I also added box colliders to both my character and gameobjects for the trigger. Rigidbody is added to the character too. But I still cannot trigger my callout menu. Please help, thanks a lot!
    Also, after I added box colliders to the gameobjects, these items still cannot stop my character. If my character bounces on the gameobject, the gameobject will slow my character down, but if I keep pushing, the character will pass the gameobject. How should I solve this problem?

    Code (CSharp):
    1. public class EnterCity : MonoBehaviour
    2. {
    3.     public GameObject EnterCityMenu;
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.             EnterCityMenu.SetActive(false);
    8.     }
    9.     void OnTriggerEnter2D(Collider2D other)
    10.     {
    11.         if (other.CompareTag("Player"))
    12.             {
    13.                 EnterCityMenu.SetActive(true);
    14.             }
    15.     }
    16.     void OnTriggerExit2D(Collider2D other)
    17.     {
    18.         if (other.CompareTag("Player"))
    19.             {
    20.                 EnterCityMenu.SetActive(false);
    21.             }
    22.  
    23.     }
    24. }
     
  2. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    230
    Your code looks correct. Make sure you've added 2D physics to your objects, not 3D physics.

    It depends on how your character's movement is implemented. It's possible that it's moving the transform directly instead of using its physics API.
     
  3. coysterry

    coysterry

    Joined:
    Jan 14, 2024
    Posts:
    5
    Could you tell me a bit more about it? Which class should I use for this? Thanks
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,871
    Either the Rigidbody or Rigidbody2D classes, the components you use to give objects physics.
     
  5. coysterry

    coysterry

    Joined:
    Jan 14, 2024
    Posts:
    5
    Thank you, also, should I use sprites or gameobjects for items acting as triggers? Does it matter?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,871
    What only matters are the requirements for OnCollisionEnter2D/Exit2D that are detailed in the documentation.