Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Petumies · Jul 28, 2015 at 12:51 AM · spawnspawningspawnpointsspawnerspawnpoint

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);
     }
     
 }
 

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
1

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

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

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


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