its always so exciting and feels good when I figure out something myself from coding without needing to ask for help.
So, I had an image that was enabled by default, and after clicking that image, the mouse cursor changed. Copy+pasted the code didnt work for when an object starts off disabled, it didnt change the mouse cursor.
So I said to myself “lets change this “void OnMouseUp()” method to “void Update()”” method instead and I said “because the game needs to update when the object gets enabled”.
Never use Update when you need to do something only one time; Update will repeat your code on each frame but you need to change the cursor only one time when you click the image. You need to find out how to make OnMouseUp work now.
Yeah now I have no idea how to make it so it only changes the cursor after clicking on the object each time
public Texture2D cursorArrow; //this will be used right when game starts
public Texture2D cursorObject;
void Start()
{
Cursor.SetCursor(cursorArrow, Vector2.zero, CursorMode.ForceSoftware);
//changes our mouse cursor right when game starts
}
void Update() //after clicking, the cursor changes to the object icon
{
if (Input.GetMouseButtonUp(0))
{
Cursor.SetCursor(cursorObject, Vector2.zero, CursorMode.ForceSoftware);
}
//}
else if (Input.GetMouseButtonDown(1)) //right click to return to the default cursor
Cursor.SetCursor(cursorArrow, Vector2.zero, CursorMode.ForceSoftware);
}
}
By the way- if you ever need to do something specifically when a component is enabled, monobehaviour has a handy “OnEnable” method that you can implement:
If you have code that needs to run once when the enable occurs, move it to the OnEnable method. Any code that needs to run every frame- leave that in Update.
This is actually a proper way of listening to mouse events. You’ve wrapped them into conditionals, so it’s all good.
Just that your explanation of “because the game needs to update when the object gets enabled” seems misleading. Update function does not “update the game”, it just runs whatever you write in it every frame. Theoretically, OnMouseUp() should work as well. As soon as the gameObject gets enabled, it starts listening for mouseUp events on that gameObject. Be mindful of OnMouseUp’s API criteria, though: “This function is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true.”
I tried to use the onenable method since my icon starts disabled first. To reiterate what needs to happen, once the icon gets enabled (gets enabled by opening a panel), clicking on it changes the mouse cursor to that icon, then right clicking returns back to the default one.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cursorTest : MonoBehaviour
{
public Texture2D cursorArrow; //this will be used right when game starts
public Texture2D cursorTest;
void Start()
{
Cursor.SetCursor(cursorArrow, Vector2.zero, CursorMode.ForceSoftware);
//changes our mouse cursor right when game starts, the default one
}
void OnEnable() //after clicking, the default cursor changes to test cursor icon
{
if (Input.GetMouseButtonUp(0))
{
Cursor.SetCursor(cursorTest, Vector2.zero, CursorMode.ForceSoftware);
}
else if (Input.GetMouseButtonDown(1)) //right click to return to the default cursor
{
Cursor.SetCursor(cursorArrow, Vector2.zero, CursorMode.ForceSoftware);
}
}
}
Maybe I misunderstood what you were talking about earlier. If you’re polling for input, then you want that to be in Update. As I mentioned, OnEnable only happens once when the component is enabled. That means that if the user presses the mouse button 10 milliseconds later than it will be too late. Rather, you want to check for input in Update which runs every frame
But, closing the panel disables the icon again. I only want the cursor to change when the icon is enabled from opening the panel and clicking on the icon