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 /
This question was closed Nov 24, 2017 at 07:26 AM by awts202 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by awts202 · Jul 31, 2017 at 04:41 PM · instantiaterandomizationc #

Random Object Spawn on Unique Locations.

Hello, I did some research for spawning random objects but they didn't seem not I wanted. For an example, I have a terrain and there is a specific locations that I want an object to spawn there.

Here is what I should do:

  1. There is more than 5 spawn locations and 1 or more objects that I want to randomly instantiate in this locations.

  2. Instantiated Random Objects should not have to spawn again in the same location point.

How do I achieve this? I really appreciate any help. Thanks.

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

Answer by Rickhouse · Jul 31, 2017 at 07:03 PM

  1. Assuming these objects are saved in your Assets, make a list of objects: List object;

  2. Add all your objects to that list List.Add(Resources.Load("RandomObjectName" as GameObject));

  3. Make a function that includes steps 4-6.

  4. Make a copy of that list and have a random number generator pick 'n' number of objects to spawn at that location

  5. Make a while loop that iterates 'n' times and contains: a random number generator that selects from 0 to the size of the copy of the list, and Instantiates that random number index, then removes that index from the list and repeats this until the while loop is over, move each object to right, left, or above the position when spawned for repeats

  6. Call this for each location

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 Gkoumas · Jul 31, 2017 at 07:45 PM

  using System.Collections.Generic;
  using System.Linq;
  using UnityEngine;
  public class SpawnObjects : MonoBehaviour {
          //specify all gameobjects to spawn in editor
          public GameObject[] SpawnableObjects;
  
          //private non editable in editor
          private Dictionary<Vector3, GameObject> _spawnedObjects = new Dictionary<Vector3, GameObject>();
          private System.Random Rand = new System.Random();
  
          //specify the spawn borders in editor
          public Vector3 MinSpawnRange;
          public Vector3 MaxSpawnRange;
  
          void Start()
          {
              for(int i=0; i < SpawnableObjects.Length; i++)
              {
                  Vector3 randPos = new Vector3(Rand.Next((int)MinSpawnRange.x, (int)MaxSpawnRange.x), Rand.Next((int)MinSpawnRange.y, (int)MaxSpawnRange.y), Rand.Next((int)MinSpawnRange.z, (int)MaxSpawnRange.z));
  
                  while (_spawnedObjects.Keys.Any(f => f.x == randPos.x && f.y == randPos.y && f.z == randPos.z))
                  {
                      randPos = new Vector3(Rand.Next((int)MinSpawnRange.x, (int)MaxSpawnRange.x), Rand.Next((int)MinSpawnRange.y, (int)MaxSpawnRange.y), Rand.Next((int)MinSpawnRange.z, (int)MaxSpawnRange.z));
                  }
  
                  Instantiate(SpawnableObjects[i], randPos, Quaternion.identity);
                  _spawnedObjects.Add(randPos, SpawnableObjects[i]);
              }
          }
  }
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

Follow this Question

Answers Answers and Comments

84 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

Related Questions

Instantiate Prefab at random times but keep 3 from spawning 2 Answers

Bug after reloading the scene 2 Answers

Random death instantiate? 1 Answer

Checking if object intersects? 1 Answer

How to instantiate the first 8 gameobjects in an array (UnityScript)? 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