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);
}
}