Search Unity

Turn Button's Toggle False From Another Script

Discussion in 'Scripting' started by SiMULOiD, Apr 25, 2020.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Hi all,
    I need to make the below script toggle= false from another script.

    I tried:

    GameObject.Find("Focus").GetComponent<FocusButton>().toggle = false;
    But this did not work - said 'it was protected.'

    Any ideas?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4.  
    5. using UnityEngine;
    6.  
    7. public class FocusButton : MonoBehaviour
    8. {
    9. public Image image;
    10. private bool toggle = false ;
    11. public void TurnFocusOn ()
    12. {
    13.      
    14.       if(!toggle)
    15. {
    16.  
    17.          
    18. toggle= true;
    19.      GameObject.Find("Focus").GetComponent<FocusOnObject>().enabled = true;
    20.    
    21.              image = GetComponent<Image>();
    22.           var tempColor = image.color;
    23.           tempColor.a = 1f;
    24.           image.color = tempColor;
    25. }
    26. else
    27. {
    28.  
    29. toggle= false;
    30.      GameObject.Find("Focus").GetComponent<FocusOnObject>().enabled = false;
    31.    
    32.              image = GetComponent<Image>();
    33.           var tempColor = image.color;
    34.           tempColor.a = .2f;
    35.           image.color = tempColor;
    36. }
    37.       }}
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You are using a private access modifier for 'toggle'. This makes 'toggle' private to the FocusButton script, limiting its visibility to other methods in the same script. Other scripts cannot see this attribute. If you want to edit it, make 'toggle' public instead, or create a public property for setting and getting it.

    On a note, you should avoid GameObject.Find since it's slow and never has to be used.
     
    SiMULOiD likes this.
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks, Yoreki, that makes sense and worked.
    Also, I hear what you’re saying regarding GameObject.Find being slow. Could you recommend a better way to find an object in a scene and then GetComponent?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    If both objects are in the scene at the start of the scene you can do the normal Drag'n Drop with a public reference thing in the editor:
    Code (CSharp):
    1. public FocusButton FocusButton; // <= drag a value into here in the inspector
    2.  
    3. void SomeMethod() {
    4.   FocusButton.toggle = false;
    5. }
    Otherwise look into Singleton patterns.
     
    SiMULOiD likes this.
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    For getting a reference, there are a few options:
    • If both objects have a permanent link and exist before runtime, you can just assign the reference through the inspector. So you'd have a public GameObject variable and drag the object you want to reference in there.
    • If you create the object yourself, you get the reference. This is true for normal objects, as well as Instantiate()
    • You can get the reference from some other source, like a collision.
     
    SiMULOiD likes this.
  6. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks for the suggestions, Yoreki!