• 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 /
avatar image
0
Question by CodeTurtle · Apr 03, 2015 at 12:59 PM · mouseclickonmousedown

Disable all mouse click event in script

Many gameobjects in my game has a OnMouseDown() event of its own. When my player is doing a certain action, I don't want the gamer to accidentally activate any of these mouse events. I could just use a boolean variable to track it down, but that would acquire me to change too many scripts in my game.

I'm wondering if there's a way to disable the mouse button for a while in script?

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

2 Replies

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

Answer by lordlycastle · Apr 04, 2015 at 04:23 AM

Two ways come to mind if you want to disable Input globally. There is no Unity’s own method, you’ll have to implement your own solution. First is that you create a wrapper for Input class which has a flag it checks before processing the input, and you call this Input class’s method instead of directly calling Unity’s. This way you’ll have more control is maybe in some situation future you don’t want to turn off all input. Second is create a persistent singleton with a flag inputAllowed, add an instance, and in every OnMouseDown, OnMouseUp etc. check that it’s set to true before continuing. Below is template class you could include in your project, it just simplified creating singletons. I would also recommend reading the link above.

 using UnityEngine;
 
 /// <summary>
 /// Persistent singleton.
 /// </summary>
 public class PersistentSingleton<T> : MonoBehaviour    where T : Component
 {
     private static T _instance;
     
     /// <summary>
     /// Singleton design pattern
     /// </summary>
     /// <value>The instance.</value>
     public static T Instance 
     {
         get 
         {
             if (_instance == null) 
             {
                 _instance = FindObjectOfType<T> ();
                 if (_instance == null) 
                 {
                     GameObject obj = new GameObject ();
                     obj.hideFlags = HideFlags.HideAndDontSave;
                     _instance = obj.AddComponent<T> ();
                 }
             }
             return _instance;
         }
     }
     
     /// <summary>
     /// On awake, we check if there's already a copy of the object in the scene. If there's one, we destroy it.
     /// </summary>
     public virtual void Awake ()
     {
         DontDestroyOnLoad (this.gameObject);
         if (_instance == null) 
         {
             _instance = this as T;
         } 
         else 
         {
             Destroy (gameObject);
         }
     }
 }

Now to create a singleton class you simply have to do

 public class InputControllerSingleton: PersistentSingleton<MonoBehaviour> {
     public bool inputAllowed{ get; set; }
     IEnumerator turnOffInputFor(float seconds){…} 
 }
 
 //Simple check when you receive input.
 if (!InputControllerSingleton.instance.inputAllowed)
     return;

There is no way to turn off input with a single line of code, you’ll have to check a condition in every input method. Use a wrapper if you want to extend the Unity’s Input class, otherwise singleton should do just fine.

Comment
Add comment · Show 4 · 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 CodeTurtle · Apr 04, 2015 at 12:47 PM 0
Share

Hmmm.....not sure if it is flawed or just too complicated for me to understand. I don't really see how this works out. But thanks for replying though! It's still good to know whether or not if such code existed.

avatar image lordlycastle · Apr 05, 2015 at 08:06 PM 0
Share

@CodeTurtle Read the link above about singletons, singletons is just a variable which can only have one instance in the game. So you create InputControllerSingleton with a variable inputAllowed, and in every On$$anonymous$$ouseDown, and On$$anonymous$$ouseUp etc. methods you check if the inputAllowed variable of the instance of InputControllerSingleton is set to true, if not then you return from the method and receive no input.

The class PersistantSingleton is just there as a template to create singletons fast, and easily.

avatar image CodeTurtle · Apr 06, 2015 at 10:55 AM 0
Share

Thank you! I guess I kind of understand now.

So this "singleton" works like a static variable, but is available in different scripts?

Anyway, I tried searching with "Disable key control" and found the same fact that there is no simple solution to this.

avatar image lordlycastle · Apr 08, 2015 at 05:48 PM 0
Share

@CodeTurtle Yes, singletons are available from any script in the game. Just use PersistentSingleton.instance (the PersistentSingleton is a placeholder) anywhere you want to access it. Singletons do have some similar characteristics to Static variables, but remember that there can only be one instance of a singleton, while a static variable can be accessed from any number of instances of that class; though it’ll have the same value.

If something is unclear just ask.

avatar image
0

Answer by Landern · Apr 03, 2015 at 01:02 PM

Sure, use a boolean to control the flow, or disable the collision component or completely disable the gameobject.

The next option is to change the way you're implementing controls/interaction in your game... revisit the logic.

You already pretty much answered your own question in your original post. If you want to disable something periodically... you will have to evaluate something that returns true or false.

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 CodeTurtle · Apr 03, 2015 at 01:13 PM 0
Share

But there are too many objects and scripts I need to change in order for this to happen, which might cause some logic disorder. I want to know if there's a easier way for me to just stop mouse button from functioning(works like unplugging the mouse).

Still thanks for answering though. :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

C# MOUSE EVENT TOGGLE 1 Answer

How to move object to the position of another object while clicked on it? 1 Answer

Why Is the game object not selected? I'd like to understand why. 2 Answers

Click for destroy Prefab, but sometime don't work 1 Answer

2D Colliders and OnMouseDown not working after build. 1 Answer


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