Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JNArcane · Nov 29, 2014 at 02:31 PM · exceptioninvoke

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JNArcane · Nov 29, 2014 at 11:20 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges