Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Der_Kevin · Jun 28, 2016 at 09:23 AM · instantiaterandomspawn

Spawn Random Object script spawns too much

Hey! So i have this Spawn Script which should spwan objects in a specific area each X seconds.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnGameObjects : MonoBehaviour
 {
     public float secondsBetweenSpawning = 0.1f;
     public float xMinRange = -25.0f;
     public float xMaxRange = 25.0f;
     public float yMinRange = -5.0f;
     public float yMaxRange = 0.0f;
     public float zMinRange = -25.0f;
     public float zMaxRange = 25.0f;
     public GameObject[] spawnObjects; // what prefabs to spawn
 
     private float nextSpawnTime;
 
     void Start ()
     {
         // determine when to spawn the next object
         nextSpawnTime = Time.time+secondsBetweenSpawning;
     }
 
     void Update ()
     {
         // if time to spawn a new game object
         if (Time.time  >= nextSpawnTime) {
             // Spawn the game object through function below
             MakeThingToSpawn ();
 
             // determine the next time to spawn the object
             nextSpawnTime = Time.time+secondsBetweenSpawning;
         }   
     }
 
     void MakeThingToSpawn ()
     {
         Vector3 spawnPosition;
 
         // get a random position between the specified ranges
         spawnPosition.x = Random.Range (xMinRange, xMaxRange);
         spawnPosition.y = Random.Range (yMinRange, yMaxRange);
         spawnPosition.z = Random.Range (zMinRange, zMaxRange);
         if ((spawnPosition.z < 4 && spawnPosition.z > -4) || (spawnPosition.x < 4 && spawnPosition.x > -4)) 
         {
             MakeThingToSpawn ();
         }
 
         // determine which object to spawn
         int objectToSpawn = Random.Range (0, spawnObjects.Length);
 
         // actually spawn the game object
         GameObject spawnedObject = Instantiate (spawnObjects [objectToSpawn], spawnPosition, transform.rotation) as GameObject;
 
         // make the parent the spawner so hierarchy doesn't get super messy
         spawnedObject.transform.parent = gameObject.transform;
     }
 }

but there is a weird bug which leads to the behavior that somethimes 3 objects get spawned, sometimes 5, sometimes 1... but it should only spawn 1 object. can someone spot the mistake?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jdean300 · Jun 28, 2016 at 09:33 AM

Inside MakeThingToSpawn, where you call MakeThingToSpawn again, you are making the function call recursive - meaning multiple calls are happening simultaneously depending on how the random numbers play out:

MakeThingToSpawn

if (numbers in a certain range)

---MakeThingToSpawn

---if (number in a certain range)

------MakeThingToSpawn

.... and so on

I'm guessing this is what you're looking for. Rather then calling the method over and over again when the position is incorrect, this just recalulates the value until it is right.

      void MakeThingToSpawn ()
      {
          //Start the vector at an invalid position
          Vector3 spawnPosition = new Vector3(0, 0, 0);
  
          //while we are not in the right range, continually regenerate the position
          while ((spawnPosition.z < 4 && spawnPosition.z > -4) || (spawnPosition.x < 4 && spawnPosition.x > -4)) 
          {
              spawnPosition.x = Random.Range (xMinRange, xMaxRange);
              spawnPosition.y = Random.Range (yMinRange, yMaxRange);
              spawnPosition.z = Random.Range (zMinRange, zMaxRange);
          }
  
          // determine which object to spawn
          int objectToSpawn = Random.Range (0, spawnObjects.Length);
  
          // actually spawn the game object
          GameObject spawnedObject = Instantiate (spawnObjects [objectToSpawn], spawnPosition, transform.rotation) as GameObject;
  
          // make the parent the spawner so hierarchy doesn't get super messy
          spawnedObject.transform.parent = gameObject.transform;
      }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Der_Kevin · Jun 28, 2016 at 12:58 PM 0
Share

perfect, thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

54 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to make a field of meteors forever, but I can tell I'm doing it horribly 0 Answers

Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers

How can I make an object clone in the time that I indicate it to appear? 0 Answers

Wont instantiate game object 1 Answer

Instantiate PreFab on trigger location 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges