I am just trying to spawn obstacle but the obstacle gets cloned every frame

using UnityEngine;

public class ObstacleSpawner : MonoBehaviour
{
[SerializeField] private GameObject obstaclePrefab; // Prefab for the obstacle
[SerializeField] private GameObject player; // Reference to the player
private bool isGenerated = false;

void Update()
{
    GenerateObstacle();
}

void GenerateObstacle()
{
    Vector3 spawnPos = new Vector3(20f, -1.5f, player.transform.position.z);
    
    if (!isGenerated)
    {
        Instantiate(obstaclePrefab, spawnPos, Quaternion.identity); // Spawn the obstaclePrefab
        isGenerated = true; // Set the flag to true to prevent further spawning
    }
}

}

Is this component on the obstacle prefab itself? If so, every time you spawn one, it will spawn another.

1 Like

yep the script was the comonent of prefab and it was causing the problem thanks for the answer.

1 Like