Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Trying to make a button enable (or disable) components, but I can't seem to be able to.

Discussion in 'UGUI & TextMesh Pro' started by tame_doggo10, Sep 3, 2022.

  1. tame_doggo10

    tame_doggo10

    Joined:
    Jul 21, 2022
    Posts:
    1
    I am trying to make a button in my game, that when collided with, will change a bool value in a component Button_Enable, that other gameObjects will sense in a component called Button_Sense. The Button_Sense component will sense that the button has been pressed, but it won't disable the components that should be disabled.

    I am new to unity, but i looked everywhere and couldn't seem to fix this problem. I used an array of public string variables and a repeat length of array line of code, then after that I disabled/enabled the component. the object will change transparency, but it won't disable the components in the array for that gameObject. Here is my Button_Sense component code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Button_Sense : MonoBehaviour
    6. {
    7.     private Button_Enable Button;
    8.     public string ButtonSensed = "Button";
    9.     private Color DisabledColor;
    10.     private Color EnabledColor;
    11.     public bool Inverted;
    12.     private Renderer Renderer;
    13.     public string[] ComponentList;
    14.     void Start()
    15.     {
    16.         Button = GameObject.Find(ButtonSensed).GetComponent<Button_Enable>();
    17.         EnabledColor = GetComponent<SpriteRenderer>().color;
    18.         DisabledColor = new Color(EnabledColor.r, EnabledColor.g, EnabledColor.b, 0.25f);
    19.         Renderer = GetComponent<Renderer>();
    20.     }
    21.     void Update()
    22.     {
    23.         if (Button.ButtonPressed && Inverted == false || Button.ButtonPressed == false && Inverted)
    24.         {
    25.             Renderer.material.color = EnabledColor;
    26.             for(int i = 0; i < ComponentList.Length; i++) {
    27.                 (gameObject.GetComponent(ComponentList[i]) as MonoBehaviour).enabled = true;
    28.             }
    29.         } else
    30.         {
    31.             Renderer.material.color = DisabledColor;
    32.             for(int i = 0; i < ComponentList.Length; i++)
    33.             {
    34.                 (gameObject.GetComponent(ComponentList[i]) as MonoBehaviour).enabled = false;
    35.             }
    36.         }
    37.     }
    38. }
    39.  
     
  2. LiamBradbury

    LiamBradbury

    Joined:
    Jun 30, 2022
    Posts:
    1
    I have this exact same problem right now and can't find a solution anywhere. I'm sure I'm doing something stupid but can't figure it out. Spent 12 hours today trying.