Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Disabling OnPointerDown?

Discussion in 'Scripting' started by HeatWave_Games, Nov 9, 2016.

  1. HeatWave_Games

    HeatWave_Games

    Joined:
    Jun 3, 2016
    Posts:
    52
    Hi,

    Is there any way to disable OnPointerDown, OnDrag, OnPointerUp, etc... functions temporarily by script?

    Or is there any workaround so I could stop them from working for a while and then turn them back on?

    Thanks in advance.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Disable the scripts that implement those interfaces
    Code (csharp):
    1.  
    2. var pointers = GetComponents<IPointerDownHandler>();
    3. if (pointers != null)
    4. {
    5.     foreach (var p in pointers)
    6.         p.enabled = false;
    7. }
    8.  
     
  3. HeatWave_Games

    HeatWave_Games

    Joined:
    Jun 3, 2016
    Posts:
    52
    Hi KelsoMRK,

    It doesn't work for me, it says (I've tried your code from Start):
    CS1061 'IPointerDownHandler' does not contain a definition for 'enabled' and no extension method 'enabled' accepting a first argument of type 'IPointerDownHandler' could be found (are you missing a using directive or an assembly reference?).

    I'd like to disable IPointerDownHandler from the same script it is used in. Can that be a problem?

    Thanks for the help.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Ah right - derp moment on my part. Cast each one to a MonoBehaviour first.
    Code (csharp):
    1.  
    2. foreach (var p in pointers)
    3. {
    4.     var mb = p as MonoBehaviour;
    5.     if (mb != null)
    6.         mb.enabled = false;
    7. }
    8.  
    You can certainly do it in the same class, in that case it'd just be
    Code (csharp):
    1.  
    2. this.enabled = false;
    3.  
    And if you need to get it again, pass true as an argument in GetComponent to return disabled components
    Code (csharp):
    1.  
    2. var poiner = GetComponent<IPointerDowHandler>(true);
    3.  
     
  5. HeatWave_Games

    HeatWave_Games

    Joined:
    Jun 3, 2016
    Posts:
    52
    Thanks a lot KelsoMRK, it works!