• 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 /
This question was closed Dec 06, 2015 at 06:43 AM by GreenTraveler for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GreenTraveler · Nov 14, 2015 at 07:25 PM · c#spawnunique

javascript to c# convert

Greetings..

I have a problem..i have 6 spawnPoints and 50 prefabs to choose from id like to spawn 6 randomly chosen prefabs without getting the same object twice from the list

below is a script i found on the net that does not repeat what is spawned but its not quite what i need

1: its not in c# and im joe lost on half its intentions 2: it spawns all the objects on one spawnpoint

would one of you kind Earthlings please help me..

  var bodyParts : GameObject[];
  
  function Start()
  {
      GenerateBodyParts();
  }
  
  
  function GenerateBodyParts() 
  {
      //make a copy of the original one
      //since we will be removing them from the array when we create them
      var bodyPartsCopy : GameObject[] = bodyParts;
  
      //loop through each bodypart
      for (var i = 0; i < bodyParts.Length; i++) 
      {
      //pick a random index
      var bodyPartsNum : int = Random.Range(0, bodyPartsCopy.Length-1);
      //instantiate the bodypart
      yield WaitForSeconds(1);
      Instantiate(bodyPartsCopy[bodyPartsNum], transform.position + Vector3(0.05,0.05,-0.9), Quaternion.identity);
  
      //now need to remove the bodypart from the array so we don't create it again
      //but we need to make it a JS array first, remove it, then convert back to a builtin array to be used by unity
      var bodyPartsJS : Array = bodyPartsCopy;
      bodyPartsJS.RemoveAt(bodyPartsNum);
      bodyPartsCopy = bodyPartsJS;
  
      }
  }

thank you

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by OncaLupe · Nov 14, 2015 at 09:24 PM

Rather than adjusting the script to work, I decided to make one from scratch. The script you found is creating and modifying a JS array every loop.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;//Allows using Lists
 
 public class RandomSpawnNoRepeat : MonoBehaviour
 {
     public GameObject[] spawnPoints;
     public GameObject[] spawnPrefabs;
 
     void Start()
     {
         SpawnItems();
     }
 
     void SpawnItems()
     {
         //Counter to prevent infinite loop
         int infLoopPrevent;
         //Pointer to an item in the prefab list
         int spawn;
         //Holds pointers to items we've spawned
         List<int> spawnedItems = new List<int>();
         //Cycle through all spawn points
         for(int i = 0; i < spawnPoints.Length; ++i)
         {
             infLoopPrevent = 1000;
             //Pick a random item in the prefab list until we find one that's not in the spawnedItems list
             do{
                 spawn = Random.Range(0, spawnPrefabs.Length);
                 --infLoopPrevent;
             }while(spawnedItems.Contains(spawn) && (infLoopPrevent > 0));
 
             //Error out if we hit an infinite loop
             if(infLoopPrevent <= 0)
             {
                 Debug.Log("Infinite loop when trying to pick a new prefab.");
                 return;
             }
 
             //Add this pointer to the list so it doesn't spawn again
             spawnedItems.Add(spawn);
 
             Instantiate(spawnPrefabs[spawn], spawnPoints[i].transform.position, Quaternion.identity);
         }
     }
 }

The script you found is also a Coroutine to delay the spawning to once a second. If you need this ability, then make the following changes to my script:

SpawnItems(); to StartCoroutine(SpawnItems());

void SpawnItems() to IEnumerator SpawnItems()

yield return new WaitForSeconds(1f); Add this before or after the Instantiate.

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

Answer by GreenTraveler · Nov 14, 2015 at 10:17 PM

Dear OncaLupe,

I can not tell you in words the joy you have given me.. you went above and beyond.. not only is that script gold in my world.. you commented each statement to help me see how it all works..I am so thankful for the time you took to help..God Bless You

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I want to spawn objects probabilistically. 0 Answers

Endless runner spawing platform spawns too many and too far apart. Help with code please? 0 Answers

Spawning loot only the first time a level is loaded 1 Answer

How do i respawn after the player dies ? 3 Answers

[SOLVED]Multiple enemies Wave Spawner 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