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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Should this work like I am expecting?

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

  1. Loading7088


    May 5, 2023
    Hello, I have a simple script on an enemy that when clicked turns the colour to a different colour and changes the tag. But for some reason, it barely works and is almost impossible to use, as weather or not it does what I want it to do seems to depend on the engines mood.. heres the code.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class Hurtful : MonoBehaviour
    6. {
    7.     public Color newColour;
    8.     public SpriteRenderer rend;
    11.     private void OnMouseEnter()
    12.     {
    13.         if (Input.GetMouseButtonDown(0))
    14.         {
    15.             // No more die, and now green.
    16.             gameObject.tag = "Safe";
    17.             rend.color = newColour;
    18.         }
    19.     }
    20. }
  2. MelvMay


    Unity Technologies

    May 24, 2013
    Well you only enter the object once and you have to be hitting the mouse button at the same time; seems not too likely.

    Shouldn't you be using OnMouseOver not simply when you very first move the mouse over a collider?

    Edit: Okay so this is essentially a duplicate of: Did you not use OnMouseDown then as suggested there?
  3. Loading7088


    May 5, 2023

    Ohhhhhhhhhh I see my issue lol. I let the autofill fill it and I didnt pay attention, I should probably not be coding while tired. thanks.
    MelvMay likes this.