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;
}
}