Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 patrickbaumi · Apr 18, 2019 at 08:10 AM · randomize

Random objects in a loop after a specific time and without back-to-back-repeats

So I want to spawn random objects ( the goodies), but without one of them spawning back to back. This is the best solution I´ve come up with but now unity freezes when I start the game (also happened with Thread.Sleep). Does anyone have an idea?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class Instantiate_House : MonoBehaviour
 {
 
     
     public Transform Spawnpoint;
     public GameObject house; 
    
     public float spawnTime;
     public float spawnDelay;
 
 
      public GameObject[] pieces;
      public int pieceSpawn;
     private int i;
     void Start()
     {
       
   InvokeRepeating("Spawn_House",spawnTime,spawnDelay);
     }
 
     
     void Update()
     {
   
     }
 
    public void Spawn_House(){
 
         pieces = new GameObject[8];
          pieces[0]=GameObject.Find("GoodieA");
          pieces[1]=GameObject.Find("GoodieB");
          pieces[2]=GameObject.Find("GoodieC");
          pieces[3]=GameObject.Find("GoodieD");
          pieces[4]=GameObject.Find("GoodieE");
          pieces[5]=GameObject.Find("GoodieF");
          pieces[6]=GameObject.Find("GoodieG");
          pieces[7]=GameObject.Find("GoodieH");
 
  
 int i= 8;
 
 pieceSpawn = UnityEngine.Random.Range (0,7);
 
       switch (pieceSpawn)
             {
                 case 0:
                 pieceSpawn = UnityEngine.Random.Range (1,7);
                 break;
                 case 1:
                 while (pieceSpawn==1)
                 {
                     if (pieceSpawn==i)
                     {
                         pieceSpawn = UnityEngine.Random.Range (1,7);
                     }
                 }
                 break;
                 case 2:
                                 while (pieceSpawn==2)
                 {
                     if (pieceSpawn==i)
                     {
                         pieceSpawn = UnityEngine.Random.Range (0,7);
                     }
                 }
                 break;
                 case 3:
                                 while (pieceSpawn==3)
                 {
                     if (pieceSpawn==i)
                     {
                         pieceSpawn = UnityEngine.Random.Range (1,7);
                     }
                 }
                 break;
                 case 4:
                                                 while (pieceSpawn==4)
                 {
                     if (pieceSpawn==i)
                     {
                         pieceSpawn = UnityEngine.Random.Range (0,7);
                     }
                 }
                 break;
                 case 5:
                                                 while (pieceSpawn==5)
                 {
                     if (pieceSpawn==i)
                     {
                         pieceSpawn = UnityEngine.Random.Range (0,7);
                     }
                 }
                 break;
                 case 6:
                                                 while (pieceSpawn==6)
                 {
                     if (pieceSpawn==i)
                     {
                         pieceSpawn = UnityEngine.Random.Range (0,7);
                     }
                 }
                 break;
                 case 7:
                 pieceSpawn = UnityEngine.Random.Range (0,6);
                 break;
                 default:
                  pieceSpawn = UnityEngine.Random.Range (0,7);
                  break;
             }
 
              i=pieceSpawn;
                 GameObject newObject = Instantiate(pieces[pieceSpawn],  Spawnpoint.position + new Vector3 (Random.Range (-50,50),0,Random.Range (-50,50)), Spawnpoint.rotation* Quaternion.Euler (90f, 0f, 0f));
         Destroy (newObject,7);
 
         
 
     }
 
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

168 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 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

Random AudioClip by their names,Random Clip 0 Answers

Randomly display an asset from Selection of assets 0 Answers

Random colors? 0 Answers

how to load random / shuffle scene without repeating current scene? (when button next level pressed) 0 Answers

How to randomize a single action among many objects PLEASE help! 1 Answer


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