Reference a script on a gameobject that is disabled in scene
Hello, I'm trying to figure out how I can reference a script and get values from it when the script is attached to a gameobject that is disabled at runtime but the gameobject becomes enabled after a user action. Currently I have a scripts on weapons the player can use, the player can use mouse-wheel to change weapons, which enables and disables the weapons depending on whatever is currently being held. If the player shoots one of these weapons an action in another script is triggered. However, currently the script is only reading if the weapon the player is holding, the enabled one, on startup shoots, the others, the disabled ones, don't trigger any actions. I'm sure it is because the game cannot find the gameobjects to read the scripts because they are disabled, but I'm not sure how to fix it or how to make the game constantly check if the gameobjects become enabled. I have this for when a weapon is switched, it enables and disables the weapons.
void SelectWeapon()
{
int i = 0;
foreach(Transform weapon in transform)
{
if(i==currentWeapon)
{
weapon.gameObject.SetActive(true);
}
else
weapon.gameObject.SetActive(false);
i++;
}
}
This is one a gameobject in the scene, it should detect when the bool from the weapon being shot is triggered. Currently the bool is being triggered but it's not being read unless the weapon was enabled by default.
public class Loud : MonoBehaviour {
public bool isLoud;
AK47 akReference;
Carbine carbineReference;
Shotgun shotgunReference;
void Start() {
akReference = GameObject.FindWithTag("AK").GetComponent<AK47>();
carbineReference = GameObject.FindWithTag("Carbine").GetComponent<Carbine>();
shotgunReference = GameObject.FindWithTag("Shotgun").GetComponent<Shotgun>();
isLoud = false;
}
void Update() {
if (akReference.akShot || carbineReference.carbineShot || shotgunReference.shotgunShot)
{
spotlightColor.color = (Color.red);
//Debug.Log("LOUD!!");
isLoud = true;
}
}
}
I'm not sure how I can make it so the bools can be detected after the gameobject has been enabled or if there is a workaround?