Getting a C# script public variable in Visual Scripting

I have a C# code that has a
public List x;
that it fills at startup.
I’d like to access that list in a Visual Scripting somewhere else. I can get (I suppose) the script with Get Component (at least it lists the C# code I want to use so I guess I do have the component after that) but if I try to get a variable from that it doesn’t find it… I’ve put that C# code in a game object that I’ve made a Scene Variable thinking this would allow me to access it in all my scripts, and I think it worked, just that I can’t understand how to extract that public variable.
Any hint of what I’m doing wrong would be more than welcome.

1 Like

UnityVS’ Get Variable and Set Variable nodes only work with UVS variable system. You can’t use them to retrieve something from a C# script.

To access the public x variable, you have to add the “Boundary” script in Project Settings under the Visual Scripting tab as a new type. Then regenerate units. This will expose all public Boundary script variables and methods to UVS and they’ll come up in fuzzy finder as dedicated nodes. Once units are regenerated, you can then search for “boundary x” in fuzzy finder to find your variable.

And UVS does not need explicit GetComponent call 99% of the time, it just needs a GameObject reference and it’ll get the right component automatically.

8 Likes

Works like a charm (without the GetComponent). Thanks a lot!

1 Like

Hello, is there a way to do the opposite. Getting the value, from an object variable that was set in visual scripting, in a C# script? I basically have a boolean variable in my visual scripting graph and I want to access it from a C# script that is on the same game object.

Hey @fabiareor I needed the same thing.

Found the “Variables” api in this documentation. Seems to work well!
(Variables node | Visual Scripting | 1.7.8)

You access it using something similar to this code. In this case I am using Graph scoped variables

Variables.Graph(flow.stack).Set("variableName", value);

This example is used inside the Execute function of a custom “Unit” or Node I have written

You can also set Application scope variables and access them with code like this:

Variables.Application.Set("variableName", value);

You can see the different “Scopes” available in this screenshot of the graph’s blackboard7879972--1002304--Screen Shot 2022-02-08 at 1.56.51 PM.png

3 Likes

You have just saved my life without knowing it… Thank you for that very helpful answer

1 Like