Search Unity

Question Mouse trigger?

Discussion in '2D' started by Loading7088, May 6, 2023.

  1. Loading7088

    Loading7088

    Joined:
    May 5, 2023
    Posts:
    9
    Hello, I am trying to make it so that when I click my mouse button while over a trigger, that the game objects tag changes.
    For example if my mouse is over a red square and I click it, I want it to change the tag to safe using a trigger. How do I make my mouse button a trigger in this case?
    Currently I have a script where if the ball rolls over the item it changes the tag
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Hurtful : MonoBehaviour
    7. {
    8.     private void OnTriggerEnter2D(Collider2D collision)
    9.     {
    10.         gameObject.tag = "Safe";
    11.     }
    12.  
    13. }
    14.  
    But that's just any and all objects with a trigger. I am planning it so that if my mouse clicks the object, it changes the tag, and the ball continues rolling but if I don't change it the ball triggers another scene.
    So I need to know two things: How do I make it so when my mouse it over a trigger and I click, it triggers whatever is OnTriggerEnter2D, and how would I make it so that if my gameObject touches a tag that isnt Safe or untagged it loads a scene from scenemanager?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    The OnTrigger family of methods is called from the physics engine, not from the mouse.

    Are you perhaps looking or something like OverlapPoint()?

    https://docs.unity3d.com/ScriptReference/Physics2D.OverlapPoint.html

    or perhaps:

    https://docs.unity3d.com/ScriptReference/Collider2D.OverlapPoint.html

    If that's not it, start with some tutorials for what you're trying to accomplish. No sense in one of us trying to guess and typing a bunch of text in this box when it's already waiting for you on Youtube.
     
  3. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    you can use OnMouseDown()

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

    or OnMouseEnter()
     
  4. Loading7088

    Loading7088

    Joined:
    May 5, 2023
    Posts:
    9