Can't pass value into onMouseEnter()

Hi
I was wondering if it was possible to pass a variable into onMouseEnter. When I try

public void onMouseEnter(int stateLevel)
{
}

I get
Failed to call function OnMouseEnter of class VRWalkingCalling function OnMouseEnter with no parameters but the function requires 1.UnityEngine.SendMouseEvents:smile:oSendMouseEvents (int)

I have a workaround where I call an intermediary function which removes the errors but was hoping for some cleaner ideas! thanks

When you have a parameter, the information in that parameter has to come from somewhere. The internal code that calls OnMouseEnter doesn’t have any additional data to give you. How are you envisioning this working?

As far as the engine is concerned, you may as well be saying:

void OnMouseEnter(int theAnswerToLifeTheUniverseAndEverything)

It just doesn’t know that.

What is stateLevel? Where does that information come from? Just get the information elsewhere, keep it in a class variable, and use it in OnMouseEnter().

i was setting it in the Editor; what i want to do is pass the variable, set on the object when i call the function in my script as it will change (the speed of movement) depending on which object I focus on

This is a screenshot from the editor where i have managed to remove the errors by first calling chosenState which then calls onMouseEnter()

 public void chosenState(string status)
    {
        statusMove = status;
        OnMouseEnter();
    }
    public void OnMouseEnter()
    {
        Debug.Log("what am i doing" + statusMove);

        if (statusMove == "walk")
        {
            speedSet = 2;
        }
        else if (statusMove == "run")
        {
            speedSet = 4;
        }
        else if (statusMove == "sprint")
        {
            speedSet = 6;
        }
        else if (statusMove == "stop")
        {
            speedSet = 0;
        }
        else if (statusMove == "reset")
        {
            speedSet = 0;
            posReset = true;
        }
        mouseHover = true;
    }

:

Ah. You’re using a function name of a builtin Unity function so I assumed you were using it as that function, but it’s not related to that usage - you’re connecting it using a UnityEvent. So this is an entirely different thing.

I think that name confusion is, in fact, your problem here. I suspect that, despite seeing an error, your previous attempt probably was working. Because as far as your event is concerned, there shouldn’t be any difference between chosenState(x) and OnMouseEnter(x) - a name is just a name. If it can call chosenState(x) it can call OnMouseEnter(x). The only time a name is special is if Unity does something like, say, attempt to hook up a convenience function call to OnMouseEnter() with no parameters when the mouse cursor overlaps the thing. So the UnityEvent was probably calling OnMouseEnter(“sprint”), but then, unrelated, Unity was trying to also call OnMouseEnter() with no parameters. The latter was triggering the error message; I think the former was probably working fine.

Try your original approach but with a different method name.

1 Like

Thanks… So basically I can copy the content from onMouseEnter() into the new function and delete onMouseEnter()?

Sure, or just change the name of it.

2 Likes

Thanks for your help