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?
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.
//
}
}
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.
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.