Search Unity

Question How can I find the variables attached to a game object?

Discussion in 'Visual Scripting' started by KliveOne, Apr 10, 2023.

  1. KliveOne

    KliveOne

    Joined:
    Jul 28, 2015
    Posts:
    25
    Hellos, Im trying figure this out, the idea is to get all the variables from a gameobject (perhaps a list) and aquire its Name,Type,Value according to the Variables output.

    I know you can do Variables.Object(gameObject).Get("variableName"); , but I would love to see if I can universally get them and do what I need with them.

    Can someone shed some light into the going about this?

    Thanks.
     
  2. KliveOne

    KliveOne

    Joined:
    Jul 28, 2015
    Posts:
    25
    For reference I was able to guess with some tinkinering

    Was having a hard time with finding the type but this base works to start.


    Code (CSharp):
    1.  
    2.         var vars = Variables.Object(gameObject);
    3.         foreach (var var in vars) {
    4.             var varName = var.name;// "Name" of variable from inspector
    5.             if (var.value is String) {
    6.                 //value is a string type
    7.             }
    8.  
    9.             if (var.value is int) {
    10.                 //value is an int
    11.             }
    12.  
    13.             if (var.value is ClassName) {
    14.                 //value is a class type of ClassName
    15.             }
    16.         }
    17.