0
votes

I am a bit confused with how i set up my scene to do this. I want to create an array of my class System. Of which contains a Vector3 position a reference to a sprite renderer and the game object. I also instantiate the game object in that position in my array creation.

I setup my array like this in an empty game object in my game world:

public System[] systemArray;
void Start () {
    systemArray = new System[totalSystems];

    for (int i = 0; i < totalSystems; i++)
    {
        systemArray[i].worldPos = new Vector3(Random.Range(min,max),Random.Range(min,max), Random.Range(minZ,maxZ));
        Instantiate(systemObject,systemArray[i].worldPos,Quaternion.identity);
    }       
}

Now my System script is not attached to a game object it is just a script that will store the data of the instantiated game objects i just made. And the code looks like this:

public class System : MonoBehaviour {

   public SpriteRenderer  spriteRenderer;
   public GameObject      gameObject;
   public Vector3         worldPos;

}

My confusion here is how do i link this all up.

The goal here is i will want to be able to destroy the game objects from the scene but i will still be able to access their Vector3, the GO and sprite renderer from System so i can re-instantiate again.

I got myself really confused so am hoping for some help on how i am suppose to set this up efficiently.

1
bit confusing, you might need to rephrase question with what you need and where is the problem. - Hari Prasad
In summary i want to create a bunch of game objects instantiate them and set their positions and also have a reference to sprite renderer so i don't lose such information when i destroy from game world because i may want to re-instantiate again at some future point. - WDUK
Just a little formal note: Don't name a class System. That is a top level .net namespace. Something like GameSystem would be better and not so easy confusable. - Gunnar B.
@GunnarB. yeah i realised that afterwards :P Thanks :) - WDUK

1 Answers

0
votes

Create a seperate class that is responsible for maintaining the systems. For best practice: The class would not expose the collection of systems directly but rather through methods to make it possible to decorate manipulation with logic.

It would look something like this:

public class SystemRepository
{
    private static List<System> _systems;

    public List<System> GetAll() { 
        if (_systems == null) {
          _systems = new List<System>()
        }

        // Further logic

        return _systems
    }

    public System GetBy(GameObject v) { ... }

    public void Add(List<System> s) { ... }

    public void DestroyBy(Vector3 v) { ... }

    ...
}

So when you need to access the Systems for adding/removing/getting them you'd do the following:

using Namespace/Of/SystemRepository

// ...

var systemRepo = new SystemRepository();

systemRepo.Add(totalSystems);

And somewhere else..

var systemRepo = new SystemRepository();

var allSystems = systemRepo.GetAll();
for (int i = 0; i < allSystems.Count(); i++)
{
    allSystems[i].worldPos = new Vector3(Random.Range(min, max), Random.Range(min, max), Random.Range(minZ, maxZ));
    Instantiate(systemObject, allSystems[i].worldPos, Quaternion.identity);
}

Note: Inside the SystemRepository I declared the list of systems to be static and only instantiate it if it is null. That means it can be only instantiated once in the lifetime of your game so that you always get the same data back no matter what instance of the repo you access. Such a thing usually would be done using a Database.