Failed to call function OnMouseDown

Hey so I was coding a little script (With no tutorial so maybe that’s why it’s wrong, I’m new to C Sharp) and this is the script:

using UnityEngine;
using UnityEngine.SceneManagement;

public class CompScriDont : MonoBehaviour
{

void OnMouseDown(Collider2D col)
{
if (col.gameObject.name == “Computer”)
{
SceneManager.LoadScene(“Computer”);
}
}

}

And this error popped up:

Failed to call function OnMouseDown of class CompScriDont
Calling function OnMouseDown with no parameters but the function requires 1.
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32)


Anyway, its not a compiler error 'cause the game still runs.
Put I won’t let me load the level when I click the item,
Please help if you can and if you can thanks so much!

Please use code tags, there is a pinned thread at the top of this forum.

OnMouseDown does not take any parameters and you cannot simply add some in while expecting Unity to do something about it.

What you need to look at is raycasting where you click the mouse and then doing something with the collider it hits (if any)

…And if this script is on the object in the game, with a collider, just remove the collider parameter from the method and remove the check for the right name (because it’s on the right object)
and then it should work… (if it’s setup that way).

Code tags are the best, for sure :slight_smile:

This is a built-in method callback and it does not have any arguments. Remove the argument if you want the built-in callback, or rename it to something else if you’re trying to make your own.