Unity 3d game

begineer in unityScripting error in my segment generator
using System.Collections;
using UnityEngine;

public class SegmentGenerator : MonoBehaviour
{
public GameObject segments; // Array of segment prefabs
[SerializeField] private int zPos = 50; // Corrected variable name (lowercase β€˜z’)
[SerializeField] private bool creatingSegment = false;
[SerializeField] private int segmentsToGenerate = 3;
private GameObject currentSegment;
private int segmentCount = 0;
public int SegmentCount => segmentCount;
public bool CreatingSegment => creatingSegment;
public int SegmentsToGenerate => segmentsToGenerate; // Fixed the property name
void Update()
{
// Check if we need to create a new segment
if (!creatingSegment && segmentCount < segmentsToGenerate)
{
StartCoroutine(SegmentGen());
}
}
public IEnumerator SegmentGen()
{
creatingSegment = true;
GenerateSegment();
yield return new WaitForSeconds(4); // Adjust the delay as needed
creatingSegment = false;
}
private void GenerateSegment()
{
if (segments.Length == 0) return;
int segmentNum = Random.Range(0, segments.Length);
GameObject segment = ObjectPooling.SharedInstance.GetPooledObject();
if (segment != null)
{
segment.transform.position = new Vector3(0, 0, zPos);
segment.transform.rotation = Quaternion.identity;
segment.SetActive(true);
zPos += 50;
segmentCount++;
currentSegment = segment;
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

public class ObjectPooling : MonoBehaviour
{
public static ObjectPooling SharedInstance;
public List pooledObjects;
public GameObject objectToPool;
public int amountToPool;
public bool shouldExpandPool = true;
void Awake()
{
if (SharedInstance == null)
{
SharedInstance = this;
}
else
{
Destroy(gameObject);
}
}
void Start()
{
pooledObjects=new List();
GameObject obj;
for(int i=0;i<amountToPool;i++)
{
obj=Instantiate(objectToPool);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for(int i=0;i<amountToPool;i++)
{
if(!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
if (shouldExpandPool)
{
GameObject obj=Instantiate(objectToPool);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
return null;
}
public void ReturnToPool(GameObject obj)
{
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public void ResetSegment(GameObject segment)
{
if (segment != null)
{
segment.SetActive(false);
ObjectPooling.SharedInstance.ReturnToPool(segment);
}
}
public GameObject GetCurrentSegment()
{
return currentSegment; // Return the currently active segment
}
}

using UnityEngine;
using System.Collections;
public class Trigger : MonoBehaviour
{
private SegmentGenerator segmentGenerator;
void Start()
{
segmentGenerator = FindObjectOfType();
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Trigger Detected: " + other.gameObject.name);
if (other.CompareTag(β€œPlayer”) && segmentGenerator != null)
{
if (segmentGenerator.SegmentCount >= segmentGenerator.SegmentsToGenerate)
{
GameObject currentSegment = segmentGenerator.GetCurrentSegment();
if (currentSegment != null)
{
segmentGenerator.ResetSegment(currentSegment);
}
}
if (!segmentGenerator.CreatingSegment)
{
StartCoroutine(SegmentGenCoroutine());
}
}
}
private IEnumerator SegmentGenCoroutine()
{
yield return segmentGenerator.SegmentGen();
}
}

pls could you format your question, it looks so messy.

2 Likes

The problem is the segments are generating in the same place , and if player enters the trigger ,it reuse the segment only once

Im gonna guess your problem lies

here

however, simple debugging would show what it is or isnt doing, and your code would be a lot easier to read if you formatted it, as half of it is in code tags half of it not, and it makes it much harder to follow whats what

1 Like