0
Answered

Difference between creating a variable in the variable window and adding a variable script to an object

Alessandro 2 years ago updated by Lazlo Bonin (Lead Developer) 2 years ago 6

As per subject, what is the Difference between creating a variable in the variable window and adding a variable script to an object?

I noticed that in a few cases if I create a variable as a script in the inspector, instead of creating the same variable in the "variable window", the script variable won't be found. Any idea why?
Not a big deal, I can keep my variables sorted in the variables window, just a curiosity...

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

For example, in this graph, if I drag n drop the inspector variable, it won't be found, if I drag n drop the scene variable, the graph will work

+1

I see your problem. You have two variables components on your object. Delete the empty one. It will always get the variables from the first variables component on the object. Can only have one set.

Got it. But what is the difference between creating a variable in the Variables manager or in the inspector?

Variables window is your current view of all variables of that scope at the time. But behaves like regular unity inspector, so you can set them up before gameplay, or by hand, not by logic.

The units in the graph will create one if it doesn't exist through logic, or set them to a new value or type during runtime. It'll be null if not already existing, before the logic happens. There is a unit to check that, 'Is Variable Defined'. So you can do something depending on that.

Pending Review

The variables window is a handy shortcut, but it doesn't store the data itself.

For example, the object tab points to the Variables script (component) that you see in the inspector.

The scene tab points to the variables component of the Scene Variables game object.

So on and so forth for the different variable scopes.