How do i make multiple instances of a script share a list

i am trying to make a procedurally generated thing where when you enter a room it generates another and in the code i am using lists and integers to know where I’m trying to place the room and if there’s already one there but the instantiated objects don’t have this information so how do i share it
here’s my code if it helps (lists and integers in question are XPos, YPos, currentroomXPos, and currentroomYPos)

using System.Collections.Generic;
using System.Reflection.Emit;
using Unity.VisualScripting;
using UnityEngine;


public class Generation : MonoBehaviour
{
    public GameObject room;
    private int currentroomXPos = 0;
    private int currentroomYPos = 0;
    int Direction;
    public List<int> YPos = new List<int>();
    public List<int> XPos = new List<int>();
    int k = 0;
    public int OffsetX;
    public int OffsetY;
    private Generation original;
    private void Start()
    {
        original = room.GetComponent<Generation>();
        XPos = original.XPos;
        YPos = original.YPos;
    }
    public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            generateRoom();
        }
    }
    public object generateRoom()
    {
        int killswitch = 0;
    start:
        killswitch++;
        for (int i = 0; i < XPos.Count; i++)
        {
            if (currentroomXPos == XPos[i] * OffsetX && currentroomYPos == YPos[i] * OffsetY)
            {
                k++;
            }
        }
        if (k == 0)
        {
            Instantiate(room, new Vector3(room.transform.position.x + currentroomXPos, 0, room.transform.position.y + currentroomYPos), Quaternion.identity);
            XPos.Add(currentroomXPos);
            YPos.Add(currentroomYPos);
        }
        else
        {
            if (Direction == 1)
            {
                currentroomXPos -= OffsetX;
            }
            if (Direction == 2)
            {
                currentroomXPos += OffsetX;
            }
            if (Direction == 3)
            {
                currentroomYPos -= OffsetY;
            }
               if (Direction == 4)
            {
                currentroomYPos += OffsetY;
            }
        }
        Direction = Random.Range(1, 4);
        if (Direction == 1)
        {
            currentroomXPos += OffsetX;
        }
        if (Direction == 2)
        {
            currentroomXPos -= OffsetX;
        }
        if (Direction == 3)
        {
            currentroomYPos += OffsetY;
        }
        if (Direction == 4)
        {
            currentroomYPos -= OffsetY;
        }
        if (killswitch == 5)
        {
            return null;
        }
        else
        if (k <= 1)
        {
            k = 0;
            goto start;
        }
        return (null);
    }
}

Make the List static so that it belongs to the type.

EDIT: Everything @spiney199 says below is 100% correct, my answer was specific on one way to have different instances share the same list

1 Like

Do they need to know the information? I don’t think so. I think you’d only need one instance of this Generation component in the scene, then your rooms inform this component to generate the next room when they’re entered.

Namely you would write another component that goes on these rooms, and use a trigger collider to get a callback when the player enters them, likely to call Generation.generateRoom() to spawn the next room. This would require passing a reference to said new component when they’re instantiated, or you can use a delegate callback alternatively.

Also why does generateRoom() return object and you just return null? And why are you using goto???

1 Like