- Home /
Random spawnpoint where only one prefab can spawn!
So I'm creating this cardgame where the cards spawn to a circle. The card should be always in random order in the circle, but when I try to make a random spawnpoint most of the cards only spawn to one or two of them. I have 52 spawnpoints where the cards should spawn but I just can't figure out how to make this work :( Here's the code:
 using UnityEngine;
 using System.Collections;
 
 public class Begin : MonoBehaviour {
     //Hertat
     public Transform H1;
     public Transform H2;
     public Transform H3;
     public Transform H4;
     public Transform H5;
     public Transform H6;
     public Transform H7;
     public Transform H8;
     public Transform H9;
     public Transform H10;
     public Transform H11;
     public Transform H12;
     public Transform H13;
     //Ruudut
     public Transform R1;
     public Transform R2;
     public Transform R3;
     public Transform R4;
     public Transform R5;
     public Transform R6;
     public Transform R7;
     public Transform R8;
     public Transform R9;
     public Transform R10;
     public Transform R11;
     public Transform R12;
     public Transform R13;
     //Ristit
     public Transform C1;
     public Transform C2;
     public Transform C3;
     public Transform C4;
     public Transform C5;
     public Transform C6;
     public Transform C7;
     public Transform C8;
     public Transform C9;
     public Transform C10;
     public Transform C11;
     public Transform C12;
     public Transform C13;
     //Padat
     public Transform P1;
     public Transform P2;
     public Transform P3;
     public Transform P4;
     public Transform P5;
     public Transform P6;
     public Transform P7;
     public Transform P8;
     public Transform P9;
     public Transform P10;
     public Transform P11;
     public Transform P12;
     public Transform P13;
     
     private GameObject[] spawnPoint;
     
     // Use this for initialization
     void Start () {
     
         Spawn();
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void Spawn (){
         spawnPoint = GameObject.FindGameObjectsWithTag("Spawn");
         GameObject randomSpawn = spawnPoint[Random.Range(0, spawnPoint.Length)];
         Instantiate(H1, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H2, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H3, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H4, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H5, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H6, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H7, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H8, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H9, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H10, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H11, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H12, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(H13, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R1, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R2, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R3, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R4, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R5, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R6, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R7, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R8, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R9, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R10, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R11, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R12, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(R13, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C1, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C2, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C3, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C4, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C5, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C6, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C7, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C8, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C9, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C10, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C11, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C12, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(C13, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P1, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P2, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P3, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P4, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P5, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P6, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P7, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P8, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P9, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P10, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P11, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P12, randomSpawn.transform.position, randomSpawn.transform.rotation);
         Instantiate(P13, randomSpawn.transform.position, randomSpawn.transform.rotation);
     }
     
 }
 
Answer by NoseKills · Jul 28, 2015 at 01:48 AM
Whenever you have objects in a list/array and want to do something to each of them once but in random order, the easiest way to go is to make a List and remove each item after you have dealt with it.
     using System.Collections.Generic;
     ...
     void Spawn ()
     {
         // array of all cards that must be spawned
         Transform[] allCards = new Transform[]{
             H1,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,H12,H13,
             R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12,R13,
             C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,
             P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13
         };
 
         List<GameObject> allSpawnPoints = 
             new List<GameObject>(GameObject.FindGameObjectsWithTag("Spawn"));
 
         // check there are equal amount of spawns and cards
         if (allSpawnPoints.Count != allCards.Length)
             throw new System.Exception(allSpawnPoints.Count + " spawnpoints found and "+allCards.Length+" cards found. Mismatch!! Can't proceed");
 
         foreach (Transform card in allCards)
         {
             // pick a random spawnpoint
             int spawnIndex     = Random.Range(0, allSpawnPoints.Count);
 
             // instantiate a card there
             Instantiate(card, 
                     allSpawnPoints[spawnIndex].transform.position,
                     allSpawnPoints[spawnIndex].transform.rotation);
 
             // remove the used spawnpoint from the list
             allSpawnPoints.RemoveAt(spawnIndex);
         }
     }
esimerkiks...
Your answer
 
 
             Follow this Question
Related Questions
How to prevent multiple spawns from a single spawn point? 1 Answer
Network spawning 1 Answer
How to spawn enemies at different locations and avoid overlapping each other 2 Answers
How do I spawn an object under another moving object at random in C#? 1 Answer
How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                