Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private float spawnLimitXLeft = -22.0f;
private float spawnLimitXRight = 7.0f;
private float spawnPosY = 30.0f;

private float startDelay = 1.0f;


private float spawnInterval;

// Start is called before the first frame update
void Start()
{
    InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
    spawnInterval= Random.Range(3, 5);
}

// Update is called once per frame
void Update()
{
}

// Spawn random ball at random x position at top of play area
void SpawnRandomBall ()
{
    int ballIndex =Random.Range(0, ballPrefabs.Length) ;
    // Generate random ball index and random spawn position
    Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);

    // instantiate ball at random spawn location
    Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
}


What I have tried:

i have tried this All Posts in Help Room - Unity Answers[^]
Posted
Updated 9-Sep-21 23:05pm
v2
Comments
Richard Deeming 10-Sep-21 5:08am    
You haven't described the problem. And just posting a link to another help forum makes this look more like spam than a real question.

Click the green "Improve question" link and update your question to include a clear and complete description of the problem, the relevant parts of your code, and the full details of the error. Remember to indicate which line of code the error relates to. Also add the full details of precisely what you have tried and where you are stuck.
j snooze 10-Sep-21 16:36pm    
if I had to guess, take the Random.Range out of your Vector3 spawnPos initialization. Make variable equal to that random range and use that in the constructor instead of the Random.Range function call.

int spawnLimitRange = Random.Range(spawnLimitXLeft, spawnLimitXRight);
Vector3 spawnPos = new Vector3(spawnLimitRange, spawnPosY, 0);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900