Search Unity

How to run a method of a component and get it's return value in visual scripting

Discussion in 'Visual Scripting' started by noahmizrahigala, Sep 14, 2021.

  1. noahmizrahigala

    noahmizrahigala

    Joined:
    Mar 8, 2021
    Posts:
    20
    I have a component called "Raycast" with a function called "GetObjectInFront" which returns a gameobject. I need a way to invoke the function from a visual script and return to a vs variable. Previously I was using Invoke to run a function but don't see a way to get the return value. Any advice?

    Thank you.
     

    Attached Files:

  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,074
    A typical workflow is to add your custom types to UnityVS node library, then regenerate nodes which will automatically create nodes for all public type members via process called reflection. This is also how you'll get your return value for the function you want to call.

    It can be done in two ways.
    1. You can decorate your class with [IncludeInSettings(true)] attribute. Then regenerate node library in Project Settings/Visual Scripting. The attribute will automatically add your custom type to the node library.
    2. Or you can do the same via GUI in Project Settings/Visual Scripting by adding a new item on the Types list. The item being your custom script. Then regenerate nodes.
    3. Your function will now come up in Fuzzy finder like any other node via keywords "Raycast.GetObjectInFront". And it'll have a return value, assuming the method has a return value.
    Bonus tip, you can assign icons to your custom scripts and UnityVS will automatically display them in graphs for your reflected method nodes.
     
    Last edited: Sep 16, 2021
    HeraldLee likes this.