How to check a public boolean across scripts ?

Hello everyone and happy New Years,

I would like to check whether a public boolean defined in script A is true in script B. I have been trying, but since the boolean is not attached to a game object I cant get to seem it to work.

public class ScriptA : MonoBehaviour
{
    public bool collision = false;
}
public class Script_B : Agent
{
    }

void update{
Gif (GetComponent<Game_Manager>().collision == false;
{
Debug.Log("Boolean From Script A was checked")
}
}

Are both scripts attached to the same GameObject?

If so it would look like this:

public class Script_B : Agent
{
void Update() {
  bool collisionValue = GetComponent<ScriptA>().collision;
  Debug.Log("Boolean from script A was " + collisionValue);
}

If they are not attached to the same GameObject, then we can discuss further depending on your use case.

1 Like

Sadly, there are not attached to the same object. This is the part I am struggling with.

Can you explain a bit what these scripts are, how they get into the scene, how many of each there are in the scene?

1 Like

Yes of course. Script A is a Game Manager script. It detects collision of a robotic arm with a target. The script itself is attached to an empty game object at the centre of the scene.
Script B is the script for the Robotic Agent. It simply takes sensory input i.e. target location as input. This script is attached to the Agent itself. Not sure if this would matter, but in the hierarchy, the Game Manager is the parent and the Agent the child.

If collision occurs in Script A, I’d like script B to check whether this collision occurred to suspend sensory input. I am not sure how I could make script B check this however. It should be noted, that a static global variable would not work, since I would like to train multiple agent in parallel, so that they should only reference their own scripts boolean.
Please let me know if this explanation is sufficient enough.

I’m a bit confused. You’re saying Script A is a Game Manager, and there’s only one of them in the scene? How does it associate a given collision with a particular Agent? Is it the agent itself which is colliding with the GameManager?

I am glad you are asking. The Game Manager is a spectator which keeps scores and takes note of events that happen in the scene. For example, if the Agent collides with a Target, then the Game Manager takes note of this. Does that make sense?

Sure that makes sense. But if that’s the case then a single boolean variable doesn’t contain enough information to convey what you are trying to convey. You need the GameManager to store a collection of which agents have collided with a target. Or maybe even which agents have collided with which targets.

1 Like

I should have noted that each agent has their own game manager. When the game manager notices a collision a boolean is set to true. I would now like the agent script to cross reference this boolean and start an actions once this is true.
From what I could tell a public static boolean would not work, since each game manager and agent would then cross reference the same variable in memory. However each Game manager and Agent should have their own reference in memory. Therefore, only a public should be enough?

Ok and how are these agents/game managers created?

The typical way to reference another component is to simply create a public variable for the other component and assign it in the inspector. So inside your Script B you would have:

public ScriptA MyGameManager;

That will create a slot in the inspector for that object. Then you just drag and drop the gane manager into it from the hierarchy, and you can use that reference in your script.

Fantastic, will I then use :

GetComponent<Game_Manager>().test_bool

or simply:

Game_Manager.test_bool

?

Neither.
You would do this:

public Game_Manager MyGameManager;

Then assign the reference in the inspector. Then you can simply do this:

bool testBoolValue = MyGameManager.test_bool;

Dragging and dropping in the inspector is important. You must do that part correctly or it won’t work.

1 Like

Thank you for taking the time. I will play around with this in addition to defining it as a public static in another script and referencing it as Script C. Have a happy New Years. :slight_smile:

Static variables are not going to help you in this case.

Happy new years!