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 StigC · Jan 12, 2016 at 12:12 AM · instantiateprefabrandomseedrng

Only instantiate random seed once

Hello world,

I have a code spawning me some blocks in a grid:

 var gridX = 16;
 var gridY = 16;
 var spacing = 10.0;
 var xPosition = 0.0;
 var yPosition = 0.0;
 var blocks : GameObject[];
  
 function Start () {
  for (var y = 0; y < gridY; y++){
  for (var x = 0; x < gridX; x++){
  Instantiate(blocks[Random.Range(0, blocks.Length)], Vector3 (x+xPosition, y+yPosition, 0) * spacing, Quaternion.identity);
  }
  }
 }


I assign the blocks in the Inspector and would like to restrict it from picking a certain prefab more than once. Is it possible to tell the instantiation to do that?

Cheers

Comment
Add comment · Show 2
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 Owen-Reynolds · Jan 12, 2016 at 12:27 AM 0
Share

Look up how to get a random list of numbers with no repeats. It's not really a Unity topic, but has been asked/answered here a few times.

avatar image StigC Owen-Reynolds · Jan 12, 2016 at 08:05 PM 0
Share

Hi again, thanks for the answer.

Right, so I tried that and it works! However, it now spawns as many prefabs as I have in my list to choose from. Even though my grid is only 2x2, It'll still spawn all 10 I have in my list. Any idea what's wrong?

 var gridX = 16;
 var gridY = 16;
 var spacing = 10.0;
 var xPosition = 0.0;
 var yPosition = 0.0;
 var blocks : GameObject[];
 
 
  function Start()
  {
      GenerateBlocks();
  }
 
  function GenerateBlocks()
  {
  //make a copy of the original one
  //since we will be removing them from the array when we create them
  var blocksCopy : GameObject[] = blocks;
 
  for (var i = 0; i < blocks.Length; i++){
  for (var y = 0; y < gridY; y++){
  for (var x = 0; x < gridX; x++){
 
  //pick a random index
  var blocksNum : int = Random.Range(0, blocksCopy.Length-1);
 
  yield WaitForSeconds(1);
  Instantiate(blocksCopy[blocksNum], Vector3 (x+xPosition, y+yPosition, 0) * spacing, Quaternion.identity);
 
 
  var blocksJS : Array = blocksCopy;
  blocksJS.RemoveAt(blocksNum);
  blocksCopy = blocksJS;
 
 }
  }
  }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by StigC · Jan 13, 2016 at 08:37 PM

Bump

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

38 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

Related Questions

Instantiate Random Prefab From Folder 1 Answer

Unity won't Instantiate PLEASE HELP! 1 Answer

Problem with Prefab - Following cursor 0 Answers

Unable to set position of instantiated prefab 1 Answer

Size of instantiated clone is different from prefab 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