Question by
joewright09 · Aug 16, 2021 at 08:34 AM ·
spawningrandom.range
Challange 2: once add random to spawnInterval only 1 ball spawns,Once I added the Random spawn interval for Challange 2 the spawn only happens once.
Heres my code, I checked with the solution and it seemed to check out but when I add the randomness to spawnInterval the ball only spawns once then stops spawning. I dont really understand why.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManagerX : MonoBehaviour
{
public GameObject[] ballPrefabs;
private float spawnLimitXLeft = -22;
private float spawnLimitXRight = 7;
private float spawnPosY = 30;
private float startDelay = 1.0f;
private float spawnInterval;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
}
// Spawn random ball at random x position at top of play area
void SpawnRandomBall ()
{
// Generate random ball index and random spawn position
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
int ballIndex = Random.Range(0, ballPrefabs.Length);
spawnInterval = Random.Range(2f, 5f);
// instantiate ball at random spawn location
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
}
Comment