GameManager Data holder

Hy guys,
I am working on a simple game architecture / strategy. Before I actually started writing lots of code I want to make sure. simple system works.

Basically I have:
Manage Class - which I thought will create Objects and will keep track of all of them in the List or Dictionary so other systems will access only this Manager to get information.
Factory Class - actually creates / instantiates GO into the scene
Controller Class - on each GO to control their behaviour
Entity Custom Class - object data holder

I have a couple of questions you might be able to help with from your experience :

  1. How Manager Class should add objects to its list if objects were created by Factory Class?
    Currently my manager class sends events to the rest

  2. Also I will have functionality within Controller class to change GO parameters (color, scale …)
    These has to be passed on back to manager.
    But I believe this will just work because I will be changing referenced objects so it will just work.?

  3. What is the best way to link actual Scene GameObject with custom Entity Class? Currently I just have GameObject field within Constructor

And here is my code
Manage Class

public class BuildingManager : MonoBehaviour
{
    public List<Building> buildings;

    public delegate void BuildingAdded();
    public static event BuildingAdded onBuildingAdded;

    public delegate void BuildingUpdate(float value);
    public static event BuildingUpdate onBuildingUpdate;

    private void Start()
    {
        buildings = new List<Building>();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            AddBuilding();
            Debug.Log("Building was created and added to the list");
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            Debug.Log(buildings.Count);
        }
    }

    public void AddBuilding()
    {
        buildings.Add(new Building());
        onBuildingAdded?.Invoke();
    }

    public void UpdateBuildingHeight(float value)
    {
        onBuildingUpdate?.Invoke(value);
    }
}

Factory Class

public class BuildingFactory : MonoBehaviour
{
    public GameObject buildingPrefab;

    // Start is called before the first frame update
    void Start()
    {
        BuildingManager.onBuildingAdded += MakeBuilding;
    }

    private void MakeBuilding()
    {
        GameObject buildingGO = Instantiate(buildingPrefab, Vector3.zero, Quaternion.identity, transform);
    }
}

Controller Class

public class BuildingController : MonoBehaviour
{
    private void Start()
    {
        BuildingManager.onBuildingUpdate += UpdateHeight;
    }

    public void UpdateHeight(float value)
    {
        transform.localScale = new Vector3(1, value, 1);
        Debug.Log("I am changing the height: " + value);
    }
}

Entity Custom Class

public class Building
{
    public string id;
    public float height;
    public GameObject buildingGeo;

    public Building()
    {

    }

    public Building(string id, float height, GameObject buildingGeo)
    {
        this.id = id;
        this.height = height;
        this.buildingGeo = buildingGeo;
    }
}

As you can see these are very simple , I just need to connect the dots between their communication. So I have a clear way forward.

  1. You could do some funky magic and use a delegate with a return value. Normally, the delegate would only return the last method in the collection, however you could do some trickery and casting as listed in this stackoverflow thread. Alternatively (and probably much easier), you could create a static collection in your BuildingManager, and have your BuildingFactorys add to it.
public class BuildingManager : MonoBehavior
{
    public static List<Building> buildings;
    //....
}

public class BuildingFactory : MonoBehavior
{
    //....
    private void MakeBuilding()
    {
        //....
        BuildingManager.buildings.Add(buildingGO);
    }
}
  1. Yes, it should work that way.
  2. I don’t quite understand what you’re asking here, since none of your other scripts use the function. Do you mean how to have the script have a reference to the GameObject it’s supposed to be attached to? I would just place the script in the prefab when creating it, so you don’t have to mess with passing around references.

Thank you @RadRedPanda
It is clear. Re Question 3.

I have custom script building which holds all the data (color , area etc). Each time I instantiate GO (prefab) I also create a new Building.
Now (for example) When I click on GO in the scene (play mode) on mouse click I want to display information about this GO its color and area. But these are coming from custom script Object

I am trying to find a way to associate one with another
Hope it is clear