Getting the game object that owns the State Machine, from a Custom Unit in C#

Hi there,

I’m new to the Visual Scripting system. One of the first things I’m trying to do is creating some custom units to drive some high level behaviour. I’d like to access the game object that owns the State Machine component that is currently running inside my Unit… is there an elegant way to do this?

here’s a code snippet…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;

public class DebugLogNode : Unit
{
    [DoNotSerialize]
    public ControlInput InputTrigger;

    [DoNotSerialize]
    public ControlOutput OutputTrigger;

    [DoNotSerialize]
    public ValueInput Message;

    protected override void Definition()
    {
        InputTrigger = ControlInput("InputTrigger", Execute);

        OutputTrigger = ControlOutput("OutputTrigger");

        Message = ValueInput<string>("Message", "Hello ");
    }

    private ControlOutput Execute(Flow flow)
    {
        Debug.Log("[Log] " + flow.GetValue<string>(Message));

        // TODO get my game object that this state machine is running
        //GameObject gameObject = ???
        return OutputTrigger;
    }
}

Thanks in advance,
George

I’ve been stuck at the same point. Coming from PlayMaker, doing this was very easy. It seems to be impossible in Bolt as far as I can tell though.

The workaround I found is to

public ControlInput inputTrigger;
public ControlOutput outputTrigger;
public ValueInput ObjectInput;
protected override void Definition()
{
   ObjectInput = ValueInput<YourType>("ObjectInput", null);
   inputTrigger = ControlInput("inputTrigger", (flow) =>
   {
      // do something with obj
      var obj = flow.GetValue<YourType>(ObjectInput);
      return outputTrigger;
   });
}

(Untested code)

And then linking up the ObjectInput to the “This” Node in the graph. It’d be much easier for me to just have access to the GameObject hosting this graph.

You can get the data, and it actually is very easy. Just need to know where it lies. You can get all sorts of data by accessing the current graph stack from your method or lambda func.

flow.stack.gameObject
4 Likes