- Home /
Invoke fails: "Trying to Invoke method: InputController.enableInput couldn't be called."
Hey all, I've used Invoke successfully in the past, but for a reason I just can't figure out, it's failing on my enableInput
function. I've searched for this exception and as far as I can tell, it has only cropped up when someone passed a misspelled method name to Invoke or tried to use it with a coroutine. I'm not familiar with coroutines, but I suppose that neither of those conditions apply in this case. Here are (what I consider to be) the relevant snippets of InputController.cs
:
public void disableInput(float timer = 0)
{
inputAllowed = false;
if (timer > 0)
{
Invoke ("enableInput", timer);
}
}
public void enableInput(float timer = 0)
{
inputAllowed = true;
if (timer > 0)
{
Invoke ("disableInput", timer);
}
}
void Update () {
...
if (mouseLeftClicked == true) {
...
disableInput (.2f);
}
...
}
When I run the game and click, disableInput(.2f)
gets called and the program enters the disableInput
method with timer > 0
. When it tried to execute the Invoke line, it throws this exception:
Trying to Invoke method: InputController.enableInput couldn't be called.
What's wrong? Thank you!
P.S. Here's the full script in case more context is needed.
using UnityEngine;
using System.Collections;
public class InputController : MonoBehaviour {
bool inputAllowed = true;
bool mouseLeftHeld;
bool mouseLeftClicked;
EntityController characterController;
CursorController cursorController;
MovementController movement;
DialogueController dialogueController;
RaycastHit2D cursorHit;
Vector3 mouseWorldPos;
void Start () {
characterController = gameObject.GetComponent<EntityController>();
cursorController = gameObject.transform.GetComponent<CursorController> ();
movement = gameObject.GetComponent<MovementController>();
dialogueController = gameObject.GetComponent<DialogueController>();
}
void cursorRaycast()
{
mouseWorldPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
cursorHit = Physics2D.Raycast (mouseWorldPos, Vector2.zero, 0);
}
public void disableInput(float timer = 0)
{
print ("Disabling input with t = " + timer);
inputAllowed = false;
if (timer > 0)
{
Invoke ("enableInput", timer);
}
}
public void enableInput(float timer = 0)
{
print ("Enabling input");
inputAllowed = true;
if (timer > 0)
{
Invoke ("disableInput", timer);
}
}
void Update () {
//print (inputAllowed);
cursorRaycast ();
getMouseStates ();
cursorController.setCursorToDefault ();
if(Input.GetKeyDown ("escape") == true)
{
Application.Quit ();
}
if (inputAllowed == true)
{
if (cursorHit.collider != null) {
if (cursorHit.transform.tag == "NPC") {
cursorController.setCursorToTalk ();
}
}
if (mouseLeftClicked == true) {
//print ("The mouse has been clicked");
movement.stopMovingTo();
if (cursorHit.collider != null) {
if (cursorHit.transform.tag == "ColliderDummy") {
if (cursorHit.transform.parent.transform.tag == "Door") {
//print ("A door has been clicked");
DoorController doorController = cursorHit.transform.parent.gameObject.GetComponent<DoorController> ();
doorController.toggleDoor ();
}
} else if (cursorHit.transform.tag == "NPC") {
dialogueController.talkTo(cursorHit.transform.gameObject);
}
else
{
movement.moveTo (mouseWorldPos);
}
}
else
{
movement.moveTo (mouseWorldPos);
}
disableInput (.2f);
}
else if (mouseLeftHeld == true)
{
movement.stopMovingTo();
movement.moveToward (mouseWorldPos);
}
if (Input.GetKeyDown ("h") == true)
{
characterController.say ();
}
}
}
void getMouseStates()
{
mouseLeftHeld = Input.GetMouseButton (0);
mouseLeftClicked = Input.GetMouseButtonDown (0);
}
public bool inputIsAllowed()
{
return inputAllowed;
}
}
Answer by khan-amil · Nov 29, 2014 at 03:49 PM
You can't use Invoke for methods with parameters.
See the reference page for Invoke. The second parameter is only the delay before calling the method.
I wasn't trying to pass the method anything via Invoke; the timer
argument was nothing but the delay before calling the method. I figured things would be okay because enableInput
doesn't need to be given any arguments (its only parameter has a default value), but I guess I can't use Invoke with a method that has parameters of any kind. Removing the timer
parameter on enableInput
allows it to be Invoked. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613172730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IndexOutOfRangeException for initial spawn of prefabs from an array 1 Answer
Exception: INTERNAL configuration error: failed to get configuration 'system.diagnostics' 1 Answer
index out of bounds 6 Answers
Null Reference exception error 2 Answers
add a jar to my project if using the jar get CLASSNOTFOUNDEXCEPTION 0 Answers