Access gameobject variable created with bolt in a prefab from script

Hi there! So I have these objects called Circle 1, Circle 2, Circle 3… (prefabs) and each one of them has a script and a flow machine with visual scripting.

In the visual scripting I created a Boolean and I want to access it from the script in the same object to check if it’s true.

I’ve followed these simple instructions

And the important part of the code is finally this:

var Negative = (bool)Variables.Object(Circle).Get("Negative");

        if (Negative){ return; }

Does not work this way because it gives me this error:

“The name ‘Circle’ does not exist in the current context”

So I solved it, like says in that link with the instructions, by adding at the beggining the object reference:

public GameObject Circle;

The error disappears but is not working. I supose it does not because the object name (that contains the script) is not only circle, they are prefabs, more than one in the scene, so one is circle 1, other circle 2, circle 3, etc.

The console error gives me this:

UnassignedReferenceException: The variable Circle of CircleController has not been assigned.
You probably need to assign the Circle variable of the CircleController script in the inspector.

Do you know a way to do that? Maybe telling the script to access the variable that is in the same object as the script? So sorry, I’m a game designer that started to study visual scripting and sometimes code seems like hieroglyph to me.

Can somebody help me? Thanks for your time and support!!

“The name ‘Circle’ does not exist in the current context” - this just means the script does not know what this Circle should be since you haven’t declared it.

Declaring the GameObject variable is the right direction, but instead of dragging the prefab asset inside of it, you need to assign the correct instance of the prefab that exists in scene’s Hierarchy. Can be done trough code or by dragging in the prefab instance that already exists in Hierarchy. In short, you can access prefab contents from prefabs that are in Hierarchy, you can’t access prefab contents from prefabs in Project Window which are basically like templates.

Your guess that the script does not work because of Circle naming is incorrect. Variables.Object requires a gameobject input, how that GameObject variable is named is not important.

“UnassignedReferenceException: The variable Circle of CircleController has not been assigned.
You probably need to assign the Circle variable of the CircleController script in the inspector.”

As for this error. It looks like the variable you declared is null, ie does not have a value so the logic fails. Drag in the prefab instance or assign it dynamically in code and it should work.

You can tell the script to look at its own GameObject by using “this.gameObject” or just “gameObject”.

1 Like

OMG, I want your brain. Ok, I think I get it. I was going to try get component but I will try using this.gameobject. Thanks!

Well, finally a simple this was the solution!

var Negative = (bool)Variables.Object(this).Get("Negative");

Huh, that’s interesting. Technically, “this” refers to the instance of the class. this keyword - C# reference | Microsoft Learn

Perhaps it’s Bolt magic that infers the correct type it’s looking for automatically like Bolt nodes do when plugging in a GameObject without an explicit GetComponent call and it still finds the right component on that GameObject.

1 Like

Well, you got me here. I presume the same… :slight_smile: