Search Unity

Resolved Getting a C# script public variable in Visual Scripting

Discussion in 'Visual Scripting' started by xbertou, Jun 27, 2021.

  1. xbertou

    xbertou

    Joined:
    May 3, 2021
    Posts:
    8
    I have a C# code that has a
    public List<Vector3> 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.

    Screen Shot 2021-06-26 at 22.19.35.png
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,062
    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.
     
    Last edited: Jun 27, 2021
  3. xbertou

    xbertou

    Joined:
    May 3, 2021
    Posts:
    8
    Works like a charm (without the GetComponent). Thanks a lot!
     
  4. fabiareor

    fabiareor

    Joined:
    Jul 30, 2019
    Posts:
    5
    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.
     
  5. HatchDev

    HatchDev

    Joined:
    Sep 7, 2015
    Posts:
    5
    Hey @fabiareor I needed the same thing.

    Found the "Variables" api in this documentation. Seems to work well!
    (https://docs.unity3d.com/Packages/com.unity.visualscripting@1.7/manual/vs-variables-reference.html)

    You access it using something similar to this code. In this case I am using Graph scoped variables
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. Variables.Application.Set("variableName", value);
    You can see the different "Scopes" available in this screenshot of the graph's blackboard Screen Shot 2022-02-08 at 1.56.51 PM.png
     
    grom27, mahesh0806 and Parzival like this.
  6. pq314

    pq314

    Joined:
    Dec 2, 2021
    Posts:
    1
    You have just saved my life without knowing it... Thank you for that very helpful answer
     
    PanthenEye likes this.