Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Game Development How to Make a Video Game Pickups Spawn Pickups Randomly

Flies spawning out of control

Everytime I click the play button in Unity, the flies start spawning out of control and I have to stop it before it crashes. I have looked back through the videos to make sure I didn't miss a step, and I have tried to figure out what the problem is. It seems to me like the condition keeps going because of there being no MAX amount but it works fine in the tutorials?? Here is my code:

using UnityEngine; using System.Collections;

public class FlySpawner : MonoBehaviour {

[SerializeField]
private GameObject flyPrefab;

[SerializeField]
private int totalFlyMinimum = 12;

private float spawnArea = 25f;

// public accessor can allow other scripts/classes can change it's value
// static variable creates a reference variable that is the same across all 
// instances of the class

public static int totalFlies;

// Use this for initialization
void Start () {

    totalFlies = 0;

}

// Update is called once per frame
void Update () {
    // while the total number of flies is less than the minimum...
    while (totalFlies < totalFlyMinimum) {

        // ...then increment the total number of flies
        totalFlies++;

        // ...create a random position for a fly...
        float positionX = Random.Range(-spawnArea, spawnArea);
        float positionZ = Random.Range(-spawnArea, spawnArea);

        Vector3 flyPosition = new Vector3(positionX, 2f, positionZ);

        // ...and create a new fly.
        Instantiate(flyPrefab, flyPosition, Quaternion.identity);
    }
}

}

Helgi Helgason
Helgi Helgason
7,599 Points

did you change the value of totalFlyMinimum in the inspector?

Nope, the value of totalFlyMinimum is 12 in the inspector.

3 Answers

Hi Andrew,

Try this, delete the script and create it again maybe is a bug

Tung Nguyen
Tung Nguyen
1,519 Points

Thank you, this works for me.

I copied the source code > delete "FlySpawner.cs" > remove "Fly Spawner Script component" in the Inspector panel and create that component again with the copied source code. It works correctly, and that seems a bug of Unity.

I'm thinking that either totalFlies++ is being read as part of the comment line above somehow (and therefore not incrementing) or the Instantiate is winding up outside the 'while' loop somehow (and running on every frame as part of Update). From the code posted, it doesn't look like either of those things is happening, but the problem sounds like something along those lines is somehow taking place.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,028 Points

I had this problem and was able to fix it by removing the script component from the game manager and making sure it was added to the Fly prefab. :)