Passing struct C#

Hi all. first post, please be gentle.

I made struct_1 in Script_1 on gameobject_1. then created an instance called currentStruct_1 that holds my data.
i want to send this currentStruct_1 to other gameobjects for reference. However, other gameobjects dont know how to build a struct_1 and therefore cant receive it. how would i do this?

public class Script_1 : MonoBehaviour {

public struct_1 currentStruct_1;

public struct struct_1
{
private string name;

//used set/get to access struct_1
};

void Awake()
{
currentStruct_1.SetName(“pete”);
}

//(edit)
public Struct_1 getCurrentStruct_1()
{
return currentStruct_1;
}

I’m really not sure what you’re asking here - perhaps explain what it is you’re trying to do rather than explaining what you think the problem is. “other gameobjects dont know how to build a struct_1 and therefore cant receive it” makes no sense at all. Also please put your code in

 code goes here

blocks.

If you’re trying to reference a struct_1 in some other class, the type name is Script_1.struct_1

Like:

public class SomeOtherClass
{
  public void PassMeAStruct(Script_1.struct_1 theStruct)
  {
     //
     // Do funky stuff with theStruct.
     // 
  }
}
2 Likes

Two options.

Reference the struct with the fully qualified name, as in Script_1.Struct_1.

Or move the struct outside of the class scope.

1 Like

I will try to be more clear.
i attempted to do what you suggest, as it sounds like what i need, but still cant get it.

public class Script_1 : MonoBehaviour {
       
    public struct_1 currentStruct_1;
       
    public struct struct_1
    {
        private string name;

    public void setName(string newName)
    {
        name = newName;
    }
   
    public string getName()
    {
        return name;
    }
    };

    void Awake()
    {
        currentStruct_1.setName("pete");
    }

    public struct_1 getCurrentStruct_1()
    {
        return currentStruct_1;
    }

now i have another GameObject, GameObject_2, that has it’s own script, Script_2, that is as follows.

public class Script_2 : MonoBehaviour {

    public GameObject ToGameObject_1;
    public Script_1 ToScript_1;
    public Script_1.struct_1 localStruct_1;

    void Awake()
    {
        ToGameObject_1 = GameObject.Find ("GameObject_1");       //connect to GameObject_1
        ToScript_1 = ToGameObject_1.GetComponent<Script_2> ();  //connect to Script_2, that has desired struct
        localStruct_1 = ToGameObject_1.getCurrentStruct_1 ();          //store desired local struct locally
        Debug.Log(ToGameObject_1.getName();                                  //print out "pete"
    }

i had hoped that ToGameObject would posses all of currentStruct_1’s data. but it is saying :
“Null UnityEngine.Debug:Log(Object)”
when i run the last debug line.
any thoughts?

This is suggesting that ToGameObject_1 is null in your Debug.Log statement. Is the game object name exactly “GameObject_1” and is it added to the scene?

GameObject.Find() will only find GameObjects that are added to the scene and set as active.

1 Like

Yes, it can find the game object just fine. and i can access all of its methods and variables other than currentStruct_1.

edit: other than getCurrentStruct_1. and it can run it, it just gives me that null afterwards.

are there any positive or negative ramifications to moving the struct outside of the class?

Its mainly a matter of preference. If a struct is only supposed to be accessible to within the class, and has no meaning outside the class, then by all means nest it. If a struct is meant to be accessible from anywhere then it shouldn’t be nested.

1 Like
 Debug.Log(ToGameObject_1.getName();

This shouldn’t even compile…

Not only that, GameObject doesn’t have a getName() function, I assume you meant:

 Debug.Log(localStruct_1.getName());
2 Likes

Ahh thank you so much. fix that error and… unnested it–is that the right verb? and now it works great!

really appreciate the help guys, i see why unity has such a good reputation with community support!