2
votes

I have a class for a building and I want to connect it to a game object of a building. In the class, I have the properties: name, level, information. I have an asset of the buildings (things like farm, castle, harbor). I have a Game class where I create the objects. The game class is used to store the objects, so they can be called through the entire game and so that the game can be saved an loaded like this.

I can't really find what I want by Googling, I also find it hard to describe what I mean.

The Building class:

[System.Serializable]
public class BuildingObject
{
    public string name;
    public int level;
    public string information;

    public BuildingObject(string information)
    {
        this.name = "";
        this.level = 0;
        this.information = information;
    }
}

This is the Game class where I create the objects. I want the created objects to be referenced inside a GameObject, so I can call properties of certain gameObjects by using them in other files and keeping progress of the level of certain gameObject.

public static Game current;

public BuildingObject church;
public BuildingObject castle;
public BuildingObject fort;
public BuildingObject farm;
public BuildingObject pub;
public BuildingObject houses;

public PlayerObject player;

public StoryObject story;

public Game()
{
    church = new BuildingObject("Informatie over de kerk");
    castle = new BuildingObject("Informatie over het kasteel");
    fort = new BuildingObject("Informatie over het fort");
    farm = new BuildingObject("Informatie over de boerderij");
    pub = new BuildingObject("Informatie over de kroeg");
    houses = new BuildingObject("Informatie over de huizen");

    player = new PlayerObject();

    story = new StoryObject();
}

I want to connect/reference the created objects inside the Game object to according GameObjects. If I have created a castle GameObject in the hierarchy, I want the properties of the castle Object inside the GameObject. If there is another better or easier way, please let me know, I am new to unity.

3

3 Answers

3
votes

Use Prefabs

You can store templates of gameObjects to be created in your hierarchy. You will need to attach your script as a Component, which can be referenced through a call to GameObject.GetComponent<BuildingObject>()

0
votes

As far as I understand, you need to inherit your Building Object from MonoBehavior:

public class BuildingObject : MonoBehavior

Then create an empty game object (prefab) and add this BuildingObject component to it. Then you can add as many buildings (those prefabs with BuildingObject component) to the scene and configure their parameters.

If I understand right, the class Game is the component on some GameObject in your scene. So then you can drag and drop those buildings (GameObjects in the scene) to your variables (references to BuildingObject) in the Game class instead of creating those BuildingObjects in code.

If you need to dynamically create new buildings in the Game class (or any other), you need to create the reference to the BuildingObject prefab in the class (and drag and drop your building prefab to this reference in the Game class):

public BuildingObject buildingPrefab;

Then you'll be able to create more new buildings (GameObjects with building component(script)) dynamically in the Game class like this:

BuildingObject newBuilding = Instantiate(buildingPrefab);

Which will add the new building as new GameObject to the scene. And then do whatever you want with those buildings :)

0
votes

You should use ScriptableObjects instead of prefabs.

ScriptableObjects are Assets that are serialized by Unity, can be edited in play mode and that are deployed to the builds.

They provide all the advantages of a MonoBehaviour, but they don't have any update flow (Update, FixedUpdate, LateUpdate and any live GamePlay methods) and don't have to be instantiated as a GameObject in the scene.

cf: https://docs.unity3d.com/Manual/class-ScriptableObject.html