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

Toggle.IsInteractable

Discussion in 'Scripting' started by pixeleif, Sep 28, 2018.

  1. pixeleif

    pixeleif

    Joined:
    Feb 9, 2016
    Posts:
    15
    Hello guys.

    I'm twisting my brain to get Toggle.IsInteracable to be switched on or off.
    Been googling like crazy but all search results are for older versions.

    I want to turn off IsInteractable if a bool is false.
    This is my simple script attached to the Toggle.
    The error I'm getting is: "Cannot assign to because it is a 'method group'"
    Anyone know what I'm doing wrong?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class checkifcantoggle : MonoBehaviour
    7. {
    8.     public SceneState sceneState;
    9.     public Toggle myToggle;
    10.  
    11.     void Update()
    12.     {
    13.         if (sceneState.GetComponent<InputModeScript>().canLock == false)
    14.         {
    15.             myToggle.IsInteractable = false;
    16.         }
    17.         else
    18.         {
    19.             myToggle.IsInteractable = true;
    20.         }
    21.     }
    22. }
    23.  
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Use myToggle.interactable instead.

    IsInteractable() is a method not a property, and should be used (with brackets) to check but not assign the value of interactable.

    You could rewrite your code as
    myToggle.interactable = !sceneState.GetComponent<InputModeScript>().canLock;
     
    pixeleif likes this.
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Thats a typical example of the backward nature of the unity API :p
     
    pixeleif likes this.
  4. pixeleif

    pixeleif

    Joined:
    Feb 9, 2016
    Posts:
    15
    Thank you so much :)
    Saved my day!
     
    Munchy2007 likes this.