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 /
avatar image
0
Question by koumeravinus · Mar 09, 2018 at 08:07 PM · movementspawningspawning problemsrandomspawningrandom spawn

Multiple spawn location with random game object in random time spawn c#

I have this codes but when I run, sometimes the gameobjects spawn at the same time and at the same place. I need help.

 public Transform player;
 public float maxTime;
 public float minTime;
 public int valuespawn = 2;
 public GameObject[] spawnPrefabs;
 public Transform[] spawnLocation;
 int currentPrefabs;
 int currentLocation;


 void Start () {
     spawner ();
 }
     
 void Update(){ 

     if (player.transform.position.x > 40)
         CancelInvoke ();
 }
 

 void spawner () {


     List<Transform> spawnPoint = new List<Transform> (spawnLocation);

     for (int i = 0; i < valuespawn; i++) {
         
         if (spawnPoint.Count <= 0)
             return;

         currentPrefabs = Random.Range (0, spawnPrefabs.Length);
         currentLocation = Random.Range(0, spawnPoint.Count);

         Transform pos = spawnPoint [currentLocation];
         spawnPoint.RemoveAt (currentLocation);

         Instantiate (spawnPrefabs [currentPrefabs], pos.position, pos.rotation);
         Invoke ("spawner", Random.Range (minTime, maxTime));

     }
 
 }


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

Answer by Xarbrough · Mar 10, 2018 at 03:05 AM

Try this:

 public class Spawner : MonoBehaviour
 {
     public Transform[] spawnPoints;
     public GameObject[] prefabs;
     public float minTime, maxTime;
     public int numberOfSpawns = 2;
 
     private float timer = 0f;
     private int lastSpawnPointIndex = -1;
 
     private void Update()
     {
         if (timer <= 0f)
         {
             SpawnItems();
             ResetTimer();
         }
         timer -= Time.deltaTime;
     }
 
     private void SpawnItems()
     {
         for (int i = 0; i < numberOfSpawns; i++)
         {
             Transform spawnPoint = GetNextSpawnPoint();
             GameObject prefab = prefabs[Random.Range(0, prefabs.Length)];
             Instantiate(prefab, spawnPoint);
         }
     }
 
     private Transform GetNextSpawnPoint()
     {
         // We want a random index from the error, but not the same as last time.
         // So we at least more one index further and stop one index before the last.
         // The % operator loops back to the beginning of zero, if the index overshoots the array length.
         int index = (lastSpawnPointIndex + Random.Range(1, spawnPoints.Length - 1)) % spawnPoints.Length;
         lastSpawnPointIndex = index;
         return spawnPoints[index];
     }
 
     private void ResetTimer()
     {
         timer = Random.Range(minTime, maxTime);
     }
 }
 
 public class SomeOtherScript
 {
     public Spawner spawner;
     public Transform player;
 
     private void Update()
     {
         // Style tip:
         // This logic of disabling the spawner should go in a separate class
         // because the spawner should only deal with spawning objects.
         if (player.position.x > 40)
             spawner.enabled = false;
     }
 }

It's not entirely clear to me what behavior you are trying to achieve, but I would create a simple timer which ticks down a fixed amount of time and then changes its next timer value via Random.Range. When the timer is up, we can spawn the desired number of objects.

To avoid spawning at the same location twice, you pick an index, which you haven't used last time (or a list of multiple indices to avoid alternating between two values randomly).

Disclaimer: I couldn't test this code, so it might have mistakes, but I'm sure it can point you in the right direction. ;)

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 xxasafxx3 · Aug 22, 2020 at 11:17 AM 0
Share

hey dis script is good but how can i make it not spawn in the same location twice ??

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

119 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My game object that spawns randomly threw out my game sometimes spawns inside a tree 0 Answers

How do you create a constantly spawning set of random missiles that move vertically down? 1 Answer

Respawn script not working! 0 Answers

Problem with instantiating the object in untiy at certain time intervals 1 Answer

Instantiate random object from Array 2 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