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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Button_Sense : MonoBehaviour
{
private Button_Enable Button;
public string ButtonSensed = "Button";
private Color DisabledColor;
private Color EnabledColor;
public bool Inverted;
private Renderer Renderer;
public string[] ComponentList;
void Start()
{
Button = GameObject.Find(ButtonSensed).GetComponent<Button_Enable>();
EnabledColor = GetComponent<SpriteRenderer>().color;
DisabledColor = new Color(EnabledColor.r, EnabledColor.g, EnabledColor.b, 0.25f);
Renderer = GetComponent<Renderer>();
}
void Update()
{
if (Button.ButtonPressed && Inverted == false || Button.ButtonPressed == false && Inverted)
{
Renderer.material.color = EnabledColor;
for(int i = 0; i < ComponentList.Length; i++) {
(gameObject.GetComponent(ComponentList[i]) as MonoBehaviour).enabled = true;
}
} else
{
Renderer.material.color = DisabledColor;
for(int i = 0; i < ComponentList.Length; i++)
{
(gameObject.GetComponent(ComponentList[i]) as MonoBehaviour).enabled = false;
}
}
}
}