NicknEma
NicknEma

Reputation: 478

Error says function requires 1 parameter when it actually requires none

In a Unity scene I want two objects to communicate via SendMessage so I don't have to do GetComponent at runtime.

Here is the SendMessage call:

 hit.transform.gameObject.SendMessage("OnSelect", SendMessageOptions.RequireReceiver);

And on another object (called Button) I have a script with this function:

 public void OnSelect()
     {
         dialogueManager.DisplayDialogue(this);
     }

When I play the game the OnSelect function runs without any problem and does everything it needs to do, but the console gives me the error 'Failed to call function OnSelect of class Button; Calling function with no parameters but the function requires 1.'

Why does it say the function requires 1 parameter when it actually requires none? Am I missing something?

Upvotes: 1

Views: 1496

Answers (1)

Molly J
Molly J

Reputation: 519

Two things:

  1. Unity's button class does already contain a function named "OnSelect" which does take one parameter. You can rename your function to something different https://docs.unity3d.com/530/Documentation/ScriptReference/UI.Selectable.OnSelect.html

  2. SendMessageOptions should be the third parameter not the second. So the syntax should be something like: hit.transform.gameObject.SendMessage("OnCustomHit", null, SendMessageOptions.RequireReceiver); https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html

Upvotes: 1

Related Questions