I figured out something myself!!!

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”.

Though update will be called every frame there-after while enabled. Is that what you want?

@SuperCrow2 This can help illuminate what Unity is going to call every frame:

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. :wink:

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:

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

Do I simply replace the void update method with the onenable method?

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.

I only want it to work every time it gets enabled you can click on the object and it changes the cursor

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.

You’re using code that you found in some tutorial? Could you post the complete script?

0 is a key not a button; did you change something to the original code?

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