Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 latsushi · Oct 31, 2018 at 01:05 AM · instantiaterandomarraysarraylist

Three Random Objects From Array Without Repeat

I'm trying to get three different objects to instantiate randomly in three locations so that every time I start the game the objects spawn in different places. As of right now, I was able to figure out how to get the objects to spawn at each location without any objects overlapping each other. However, the problem I have now is that the same object spawns three times.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RandomItemsThree : MonoBehaviour {
 
     public GameObject[] slideSpots;
     public GameObject[] objects;
     GameObject randomObjects;
     GameObject firstObject;
     GameObject secondObject;
     GameObject thirdObject;
 
     int objectCount;
     int spawnCount;
 
     private void Start()
     {
         MyMethod();
     }
 
     void MyMethod ()
     {
         int spawn = Random.Range(0, slideSpots.Length);
         int stuff = Random.Range(0, objects.Length);
 
         randomObjects = objects[stuff];
 
         firstObject = GameObject.Instantiate(randomObjects, slideSpots[0].transform.position, Quaternion.identity);
         secondObject = GameObject.Instantiate(randomObjects, slideSpots[1].transform.position, Quaternion.identity);
         thirdObject = GameObject.Instantiate(randomObjects, slideSpots[2].transform.position, Quaternion.identity);
     }
 }
 
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
0

Answer by Jordash · Oct 31, 2018 at 01:43 AM

You're picking a random number with

          int stuff = Random.Range(0, objects.Length);

Then assigning randomObjects as the index of stuff into the objects array

 randomObjects = objects[stuff];

But because you only generate stuff as a random number once and assign randomObjects once it will always be the same random number and therefore same object. You'll need to have a copy of that code between each spawn for a new random number and object each time.


As a side note, if you do not want duplicates you can do a while loop for the random number generator checking against the last numbers, or copy the array and do a Fisher-Yates shuffle then pick the first three, or randomly pick from a list and remove spawns/objects when chosen. Make sure you check there are more than three items in the array for these approachs though.

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

119 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

Related Questions

Three Spots For Three Random Objects 1 Answer

Instantiate random object from Array 2 Answers

Instantiate a random item at a random position 1 Answer

can you instantiate random prefabs from the resource folder? 2 Answers

How do you spawn a random object from an array. 2 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