- Home /
How to share a game's element data
This is largely a question about the best approach to organising data, and partially about the best approach to sharing data across classes.
In my 2D game, I'm dynamically generating a room made of 2D elements (I'm creating all the gameobjects via code as opposed to adding them in unity, such that I can generate the content and layout of the rooms on the fly).
Let's say, within this room I'm creating a chair. You can 'examine' this chair element to return a descriptive string, e.g. 'It's made of wood'.
I assumed the approach would be to create a public struct, e.g.
public struct ChairInfo
{
private string chairExamine;
public string examine
{
get
{
return chairExamine;
}
set
{
chairExamine=value;
}
}
}
Then upon adding the chair to the scene (make the game object with a texture component to display it within the 'game world') I should create an instance of the struct to set the 'examine' text for the char:
public class furniture_chair {
public void CreateChairElement()
{
//<snip: code primarily creating the chair game object (chairObject)>
ChairInfo chairinfo = new ChairInfo ();
chairinfo.examine = "It's a wooden chair.";
}
}
However, the instance of this struct (chairInfo) is private to the class it's created within and can't be referenced from elsewere. I.e. if elsewhere I want to read the examine, I naturally won't have access to that instance of the struct.
E.g. in the following how would I access the examine string relating to the chair (simplifying again)
public class MapLocationSelection : MonoBehaviour {
void OnMouseDown()
{
//identifying that it's the chair object created
if (gameObject.name == "chairObject")
{
//get the examine/description information for the chair
//how ?????
}
}
}
1) Should I make the instance of the struct chairInfo public and available to other classes? (not sure how I can? Is it possible?)
2) Should I not dynamically create the struct and instead have a set of placeholder public strings that I populate depending on what 'furniture' is added to the room? (e.g. public string furniture_examine_1, public string furniture_examine_2, etc for the 5 to 10 pieces of furniture that may well be in the room)
3) Should I be approaching this another way?
Answer by mattyman174 · Feb 22, 2014 at 11:43 AM
Do you even need to wrap the Property into a Struct?
Then you can just access it from the public Property by getting a reference to the script component through the GameObject.
gameObject.GetComponent<ChairScript>().examine;
Is that what your asking?
Thank you!
I've just tried that and it's working perfectly fine.
This is no doubt a sign of me getting to grips with C# having not coded for some time. Attaching a script as a component of an object in order to store information about that object seemed like a bit of a 'hacky' approach but I guess it's not.
Thanks again.
If the ChairInfo is long lived (last for more than 1 frame) then make it a class not a struct. Structs are great for variables that are mostly created as locals and then not used again (like Vector3) but are less efficient if they are used many times as a struct's whole set of values is copied every time you return it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134333im_/https://answers.unity.com/themes/thub/images/avi.jpg)