Search Unity

Resolved Access gameobject variable created with bolt in a prefab from script

Discussion in 'Visual Scripting' started by Dudusstar, May 9, 2021.

  1. Dudusstar

    Dudusstar

    Joined:
    Nov 19, 2020
    Posts:
    59
    Hi there! So I have these objects called Circle 1, Circle 2, Circle 3... (prefabs) and each one of them has a script and a flow machine with visual scripting.

    In the visual scripting I created a Boolean and I want to access it from the script in the same object to check if it's true.

    I've followed these simple instructions

    https://indiedragoness.dev/2020/07/...s-from-c-scripts-unity3d-bolt-micro-tutorial/

    And the important part of the code is finally this:

    Code (csharp):
    1.  
    2. var Negative = (bool)Variables.Object(Circle).Get("Negative");
    3.  
    4.         if (Negative){ return; }
    5.  
    Does not work this way because it gives me this error:

    "The name 'Circle' does not exist in the current context"

    So I solved it, like says in that link with the instructions, by adding at the beggining the object reference:

    Code (csharp):
    1.  
    2. public GameObject Circle;
    3.  
    The error disappears but is not working. I supose it does not because the object name (that contains the script) is not only circle, they are prefabs, more than one in the scene, so one is circle 1, other circle 2, circle 3, etc.

    The console error gives me this:


    UnassignedReferenceException: The variable Circle of CircleController has not been assigned.
    You probably need to assign the Circle variable of the CircleController script in the inspector.

    Do you know a way to do that? Maybe telling the script to access the variable that is in the same object as the script? So sorry, I'm a game designer that started to study visual scripting and sometimes code seems like hieroglyph to me.

    Can somebody help me? Thanks for your time and support!!
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    "The name 'Circle' does not exist in the current context" - this just means the script does not know what this Circle should be since you haven't declared it.

    Declaring the GameObject variable is the right direction, but instead of dragging the prefab asset inside of it, you need to assign the correct instance of the prefab that exists in scene's Hierarchy. Can be done trough code or by dragging in the prefab instance that already exists in Hierarchy. In short, you can access prefab contents from prefabs that are in Hierarchy, you can't access prefab contents from prefabs in Project Window which are basically like templates.

    Your guess that the script does not work because of Circle naming is incorrect. Variables.Object requires a gameobject input, how that GameObject variable is named is not important.

    "UnassignedReferenceException: The variable Circle of CircleController has not been assigned.
    You probably need to assign the Circle variable of the CircleController script in the inspector."

    As for this error. It looks like the variable you declared is null, ie does not have a value so the logic fails. Drag in the prefab instance or assign it dynamically in code and it should work.

    You can tell the script to look at its own GameObject by using "this.gameObject" or just "gameObject".
     
    Last edited: May 9, 2021
    Dudusstar likes this.
  3. Dudusstar

    Dudusstar

    Joined:
    Nov 19, 2020
    Posts:
    59
    OMG, I want your brain. Ok, I think I get it. I was going to try get component but I will try using this.gameobject. Thanks!
     
  4. Dudusstar

    Dudusstar

    Joined:
    Nov 19, 2020
    Posts:
    59
    Well, finally a simple this was the solution!

    Code (csharp):
    1.  
    2. var Negative = (bool)Variables.Object(this).Get("Negative");
    3.  
     
  5. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    Huh, that's interesting. Technically, "this" refers to the instance of the class. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this

    Perhaps it's Bolt magic that infers the correct type it's looking for automatically like Bolt nodes do when plugging in a GameObject without an explicit GetComponent call and it still finds the right component on that GameObject.
     
    Dudusstar likes this.
  6. Dudusstar

    Dudusstar

    Joined:
    Nov 19, 2020
    Posts:
    59
    Well, you got me here. I presume the same... :)