Referencing a variable from another script by another variable

So, I’ll try my best to explain this. I am making a 2D game. I’ve got a Player, who has a script that manages some bools which are falsified/trueified by various means (picking up an item, interacting with a specific thing, etc).

I have a parent GameObject with a Collider2D set to trigger, which holds several children. There is a script attached to the parent that holds information about all it’s children. The parent also has several scripts, with which to determine, what children should be disabled/enabled in hierarchy, based on what bools are true or false in the Player Bool script. So if the Player enters the parent’s trigger, the parent looks at the player’s bools and enables/disables it’s children accordingly.

This is the basic idea, and I have written all the scripts for it. However, I’m stuck at one thing. You’ll see it in the script.

This is the script attached to the player with all the bools:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBools : MonoBehaviour
{
    public bool hasKey;
    [Space(15)]
    public bool BarKeep_IntroDone;
    public bool lockerLocked;
    public bool Barkeep_briefingDone;
    public bool Barkeep_QuestGiven;
    [Space(15)]
    public bool bottleFound;


    public void SetBool(string boolName, bool value)
    {
        switch (boolName)
        {
        case "hasKey":
            hasKey = value;
            break;
        case "lockerLocked":
            lockerLocked = value;
            break;
        case "bottleFound":
            bottleFound = value;
            break;
        case "BarKeep_IntroDone":
            BarKeep_IntroDone = value;
            break;
        case "Barkeep_briefingDone":
            Barkeep_briefingDone = value;
            break;
        case "Barkeep_QuestGiven":
            Barkeep_QuestGiven = value;
            break;
        }
    }
}

Here’s the parent’s script that holds the info on it’s children:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoolCheckParent : MonoBehaviour
{
    public GameObject[] children;


    public void DisableChildren()
    {
        foreach (GameObject children in children)
        {
            children.SetActive(false);
        }
    }
}

And finally, here’s the script that should make the magic happen:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBoolChecker : MonoBehaviour
{
    public string boolToCheck;
    public GameObject childToEnable;
    [Space(20)]
    public GameObject player;

    PlayerBools pBools;
    BoolCheckParent bcParent;

    void Start()
    {
        bcParent = this.GetComponent<BoolCheckParent>();
        pBools = player.GetComponent<PlayerBools>();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            bool playerBoolToCheck = (bool) pBools.GetType().GetField(boolToCheck).GetValue(pBools); 

Here is where the problem is. I need to clarify that boolToCheck is supposed to be the bool from the Player Bool script that has the same name as boolToCheck (assigning the name to that is done in the inspector). I have no idea how to do this and this line is made completely by ChatGPT and it doesn’t work. The Debug.Log below doesn’t get printed either.

        Debug.Log(playerBoolToCheck);

        if (playerBoolToCheck)
        {
            bcParent.DisableChildren();
            childToEnable.SetActive(true);
        }
    }
}

}

I should note that this is my very first game and I am very much a beginner to Unity. I managed to make pretty much everything in this game with just tutorials and ChatGPT and so far, I succeeded. But I can’t find a solution to this problem. I really hope that I’ve made the problem clear and easy to understand… It’s kinda hard to explain.

I hope someone can help me :slight_smile:

Chatgtp is a useful tool, but doesn’t always give you code that makes sense, or does what you intended. If you are at a beginner level in c# and unity you will have no way of knowing if the code it outputs does what you want. I highly recommend learning the basics of c# and unity before you continue in this way, as if you can’t understand the code it gives you, you can’t work with it. You question itself also doesn’t seem to make much sense so I think you may be misunderstanding how variables work in c#. I recommend following some beginner c# tutorials to get some foundational knowledge first.

,I want you to make sure that the gameObject tag that you hit is “Player”
Try printing other in OnTriggerEnter2D
void OnTriggerEnter2D(Collider2D other){
Debug.Log(other.transform.tag);
}

,I want you to make sure that the gameObject tag that you hit is “Player”
Try printing other in OnTriggerEnter2D
void OnTriggerEnter2D(Collider2D other){
Debug.Log(other.transform.tag);
}