Why is this code not working ?
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public GameObject obstaclePrefab;
private Vector3 spawnPos = new Vector3( 25,0, 0);
private float startDelay = 2;
private float repeatRate = 2;
private PlayerController playerControllerScript;
void Start()
{
InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
playerControllerScript =
GameObject.Find("Player").GetComponent
}
}
void SpawnObstacle()
{
if (playerControllerScript.gameOver == false) {
Instantiate(obstaclePrefab, spawnPos, obstaclePrefab.transform.rotation);}}
// Update is called once per frame
void Update()
{
}
KarlPosted May 17, 2025, 7:23 PM
I think that worked ! Such a small error and I need to look out for those. Thanks
Emily FosterPosted May 16, 2025, 7:45 PM
I can see a small issue in the code snippet you provided that might be causing it not to work as intended. The `SpawnObstacle` method is currently placed outside the `SpawnManager` class due to a mismatched closing brace. This can lead to the method not being recognized as part of the class and not being called as expected.
To fix this, you should move the `SpawnObstacle` method inside the `SpawnManager` class. Here is the corrected code snippet for better clarity:
By fitting `SpawnObstacle` method within the `SpawnManager` class, it should now be correctly called during the game execution if other parts of the code are in order. If you make this adjustment and the code still does not work as expected, there might be other factors at play, so additional debugging would be needed. Let me know if you need further assistance or insight on this matter!