Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 VigorousApathy · Jan 11, 2013 at 09:55 PM · instantiatearrayrandomloop

Puzzle + Grid Instantiate - Random

Hey guys, the script below works fine, but I'm wondering how I could add more control in the way things are created randomly.

Script: The script below instantiates GameObjects per an X and Y grid. In the instantiation process, a random number is selected from the puzzleGridArray.length., which then assigns it a X and Y coordinate to go to. Pretty straightforward instantiate coding.

Problem: Sometimes the same prefab is created 3 times in a row, which is what I'd like to avoid when the board is created. The point of the game is to eliminate objects, so when this happens, the 3 destroy themselves immediately when the game starts. I'd like to even take this one step further where the prefab will not instantiate itself vertically in 3s as well (Which I think is even harder).

Question: How can I add layers of control to where if Random picks the same object 3 times in a row, it switches to another one instead and continues on its merry way?

 #pragma strict
 //Grid Script
 
 //Inspector Variables
 var gridPrefab        : Transform;
 
 var gridX            : int = 8;
 var gridY            : int = 10;
 
 var puzzleX            : int = 8;
 var puzzleY            : int = 5;
 
 var gridArray         : Transform[,] = new Transform[gridX, gridY];
 var puzzleArray     : Transform[,] = new Transform[puzzleX, puzzleY]; 
 
 var puzzleTypeArray    : GameObject[];
 
 function Start () 
 {
     SpawnGrid();
     SpawnPuzzlePieces();
 }
 
 function SpawnGrid()
 {
     for(var i = 0; i < gridX; i++)
     {
         for(var j = 0; j < gridY; j++)
         {
             Instantiate(gridPrefab, Vector3(j, i, transform.position.z), Quaternion.identity);
         }
     }    
 }
 
 function SpawnPuzzlePieces()
 {    
     for(var i = 0; i < puzzleX; i++)
     {
         for(var j = 0; j < puzzleY; j++)
         {
             Instantiate(puzzleTypeArray[Random.Range(0, puzzleTypeArray.Length)], Vector3(j, i, transform.position.z), Quaternion.identity);
         }
     }
 }
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 VigorousApathy · Jan 11, 2013 at 10:02 PM 0
Share

I don't want to quickly answer my own question after just posting it, but I may have just solved it (I'll wait for others to confirm). I can create a switch that generates a random int and the cases can change the number depending on what's picked.

So, if 0 is picked, the range 1-4 is only available, and so on.

avatar image Vonni · Jan 11, 2013 at 10:25 PM 0
Share

Sure, sound like a good solution :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by VigorousApathy · Jan 12, 2013 at 12:30 AM

http://docs.unity3d.com/Documentation/Manual/RandomNumbers.html

Comment
Add comment · Show 1 · 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 Wolfram · Jan 12, 2013 at 01:55 AM 0
Share

Nice! Didn't even know such a (detailed) doc page existed for random numbers. It essentially contains everything I said, and more.

avatar image
1

Answer by Wolfram · Jan 11, 2013 at 10:21 PM

You can use weighted random numbers instead of purely random ones (see accepted answer in link).

The pseudocode explains the principle. For a dynamic version, you would initialize all weights to 1 (not 0!) and replace the return i with:

 if(...){
     for(int j=0; j<num_choices; j++)
         choice_weight[j]++;          // increase weight of all numbers, to increase chance they'll be picked the next turn...
     choice_weight[i]=0; // ...but *reset* the weight of our current number, so it won't be picked again
     return i;
 }

EDIT:

This ensures that:

  • the same number won't be picked twice in a row

  • numbers just picked recently have lower probabilities to be picked again

  • numbers not picked increase their chances to be picked the next turn

  • it is much more likely that all numbers are picked relatively evenly distributed with much fewer samples, as opposed to a purely random approach

You can even influence the probability for certain numbers, for example by initializing then with a value >1 (but then you'd have to handle the code pictured above differently, because it will override the initial weights over time.

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

10 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

Related Questions

instantiate a random object from multiples via 'tag' 1 Answer

How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer

multiple object in multiple spawn Point with out repeat 0 Answers

Randomly instantiate objects from array without choosing the same item twice. 2 Answers

OverlapSphere for parallel arrays 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