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 /
  • Help Room /
avatar image
0
Question by bonez9oh5 · Apr 17, 2017 at 06:02 AM · c#instantiatespawnmultiple objects

Spawn a random object on more than one position

I have 5 spawn points and three game objects, my goal is to spawn a random object at each of the locations. so far i have a script that will randomly select one of the three objects and spawn it at one of the 5 locations at random. I could use a bit of help to convert the script to select a different object at each spawn point.

here is what i have so far

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameManager : MonoBehaviour { 
 //create an array of spawn points, assigned in inspector 
 public Transform[] collectableSpawnPoints = new Transform[5];
 
   //create an array of collectables to choose from
      public GameObject[] items = new GameObject[3];
  
      // Use this for initialization
      void Start () {
          SpawnCollectables();
      }
      
      // Update is called once per frame
      void Update () {
          }
  
     //selects spawn point
     public Transform GetCollectableSpawnPoint()
      {
          //randomly selects a point out of the array
          int index = Random.Range(0, collectableSpawnPoints.Length);
          //returns the selected point
          return collectableSpawnPoints[index];
      }
  
      //selects object to spawn
      public GameObject GetCollectable()
      {
          //selects one of the items from the array
          int index = Random.Range(0, items.Length);
          //returns the object selected
          return items[index];
      }
  
      // spawns the random object on the random point
      public GameObject SpawnCollectables()
      {
          //selects the spawn point
          Transform spawnPoint = GetCollectableSpawnPoint();
          //selects the object
          GameObject collectable = GetCollectable();
          //creates the object selected on the point selected
          GameObject c = Instantiate(collectable, spawnPoint.position, spawnPoint.rotation) as GameObject;
         //spawns the object
          return c;
      }


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

· Add your reply
  • Sort: 
avatar image
1

Answer by Skyking · Apr 17, 2017 at 08:50 AM

Following your example, because you are spawning at random locations anyway, there is no need to also spawn a random item. So you can just iterate the items using an indexer to make sure each spawned item is unique. I have edited your code as an example.

It will start at a random index, and then proceed from that index to select items.
Your script however will only spawn one random item. If you want to spawn all the items, I included a method to do so. private void SpawnAllCollectables()

  public class GameManager : MonoBehaviour { 
  //create an array of spawn points, assigned in inspector 
  public Transform[] collectableSpawnPoints = new Transform[5];
  
    //create an array of collectables to choose from
   public GameObject[] items = new GameObject[3];

     //keeps track of the items spawn index
     private int itemIndex = 0;
   
       // Use this for initialization
       void Start () {
           itemIndex = Random.Range(0, items.Length);
           SpawnCollectables();
       }
       
       // Update is called once per frame
       void Update () {
           }

      //spawns all the collectables from the items array
      private void SpawnAllCollectables()
      {
          //iterates through the number of collectables in items array
          for (int i = 0; i < items.Length; i++)
          {
              //spawns a unique collectable
              SpawnCollectables();
          }
      }
   
      //selects spawn point
      public Transform GetCollectableSpawnPoint()
       {
           //make sure the item index is within the item array range
           itemIndex = itemIndex % items.Length;
           //returns the selected point
           return collectableSpawnPoints[itemIndex++];
       }
   
       //selects object to spawn
       public GameObject GetCollectable()
       {
           //selects one of the items from the array
           int index = Random.Range(0, items.Length);
           //returns the object selected
           return items[index];
       }
   
       // spawns the random object on the random point
       public GameObject SpawnCollectables()
       {
           //selects the spawn point
           Transform spawnPoint = GetCollectableSpawnPoint();
           //selects the object
           GameObject collectable = GetCollectable();
           //creates the object selected on the point selected
           GameObject c = Instantiate(collectable, spawnPoint.position, spawnPoint.rotation) as GameObject;
          //spawns the object
           return c;
       }


Comment
Add comment · Show 3 · 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 bonez9oh5 · Apr 17, 2017 at 10:06 AM 0
Share

I think you misunderstood. I'm trying to eli$$anonymous$$ate the random spwan point and simply spawn 1 random item on all 5 spawn points ins$$anonymous$$d of 1 random item at 1 of the 5 points. However your answer might have unintentionally given me what I need.

avatar image Skyking bonez9oh5 · Apr 17, 2017 at 11:25 PM 0
Share

Gotcha. In that case you would just need to iterate the spawn points and select one random collectable to spawn. So something like:

 // will spawn one random collectable on each of your spawn points
 private void SpawnCollectables()
 {
     // first pick what random collectable you want to spawn
     GameObject randomCollectable = items[Random.Range(0, items.Length)];
     // next iterate all the spawn points, and spawn that item on each of them
     foreach (Transform spawnPoint in collectableSpawnPoints)
     {
         Instantiate(randomCollectable, spawnPoint.position, spawnPoint.rotation);
     }
 }

avatar image bonez9oh5 Skyking · Apr 18, 2017 at 03:18 PM 0
Share

That code may be simple but is a thing of beauty. Thanks! I just wished I saw it before I figured something else out. I ended up changing things into a for loop and used my "i" iteration in collectableSpawnPoints[i] to run the randomization and spawn for each spawn point. Giving a random item at each spawn.

avatar image
0

Answer by Cuttlas-U · Apr 17, 2017 at 06:29 AM

hi;

first of all u don't really need to create another function for every lane of your code that may just make u more confuse about what u are going to di , you can do it simply like this in 1 lane;

   public GameObject SpawnCollectables()
     {
       
          GameObject c = Instantiate(items[Random.Range(0, items.Length)], collectableSpawnPoints[Random.Range(0, collectableSpawnPoints.Length)].position, spawnPoint.rotation) as GameObject;
         //spawns the object
         return c;
     }

for your question do u want to spawn all the balls randomly in your Transform ? if that's the idea then u don't need to randomly select the balls u can just write a loop and go through all of your balls and Instantiate them;

Comment
Add comment · Show 2 · 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 bonez9oh5 · Apr 17, 2017 at 10:13 AM 0
Share

First off, please use proper English when answering to avoid further confussion. Second, I have the code arranged like this for a reason, the only changes im interested in are ones that make the change in looking for, but thanks for the tip. As for my question, as I stated I want to spawn a random item at all of my speak points ins$$anonymous$$d of one of the randomly selected spawn points.

avatar image bonez9oh5 · Apr 18, 2017 at 03:20 PM 0
Share

The reason for the higher number of functions is for debugging ease in the first steps of figuring out how to work it, as well as reusability, as I may choose to implement those functions elsewhere in later assignments.

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

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

Problem with Instantiated clones 1 Answer

What is the most efficient way to spawn in a lot of cubes, like 10k? [c#] 2 Answers

How can I make an object clone in the time that I indicate it to appear? 0 Answers

[c#] Increase speed of instantiating over time!? 1 Answer

Spawning prefabs randomly within a rectangle 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