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 DChristy87 · Apr 29, 2014 at 07:23 PM · c#arrayssorting layerssorting order

Complicated Question about Arrays and Duplicate Sorting Orders!

So I'm trying to spawn a random number, of randomly selected items from an array of prefabs. Then Place them in random positions with appropriate sorting order to their z position. It works okay, except on line 30, the random number is sometimes duplicated, this leads to two or more items with the same position and sorting order which causes weird visual for when they're spawned. Here is the full Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEditorInternal;
 using System.Reflection;
 
 public class Master : MonoBehaviour 
 {
     public List<GameObject> allPrefabs = new List<GameObject> ();
     List<GameObject> spawnList = new List<GameObject> ();
     List<int> number = new List<int> ();
     int z;
 
     void Start () 
     {
         //Take the list of Prefabs and shuffle it
         for (int r=0; r<allPrefabs.Count; r++) 
         {
             GameObject temp = allPrefabs[r];
             int randomIndex = Random.Range(r, allPrefabs.Count);
             allPrefabs[r] = allPrefabs[randomIndex];
             allPrefabs[randomIndex] = temp;
         }
         //Select a random amount of items to spawn, spawn each prefab, add it to a new Array for future actions
         int i = Random.Range (2,allPrefabs.Count);
         for (int n=0; n<i; n++) 
         {
             GameObject temp = allPrefabs[n];
             z = Random.Range(-3,4);
             Renderer renderer = temp.GetComponent <SpriteRenderer>();
             renderer.sortingOrder = -z;
             GameObject currentSpawn = Instantiate(temp,new Vector3(Random.Range (-2,2),-1,z) , Quaternion.identity) as GameObject;
             spawnList.Add (temp);
         }
 
     }
 
     void Update () 
     {
         
     }
 }
 //Need to make sure that a sortingOrder is used only once to prevent weird placement

Now the trick is, I'm limited on z space, as the rooms that these objects are spawning in are limited to the -3 and 4 z axis BUT I want to spawn as many as 20 to 30 objects So I need a big range of sorting orders that will also correlate with each items z position to each other. Is there a fix that anyone can think of?

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 robertbu · Apr 30, 2014 at 01:59 AM 0
Share

@Dameon_'s suggestion are two of the typical solutions. The third typical solutions is to put the numbers (in order) in a generic List. As the numbers come out (selected randomly), delete the entry. A fourth is to put the numbers in an array in order, then shuffle the array. The now shuffled numbers are pulled out in order.

avatar image DChristy87 · Apr 30, 2014 at 05:44 PM 0
Share

The problem is that there is an unset amount of spawned objects until the int i is selected from a random range between at $$anonymous$$imum of 2 and max of the total count of allPrefabs. So unless I'm misunderstanding, I can't create a list or array with the numbers to choose from. I may end up having a list of 100's of items for the int i to be selected from. (Probably won't but I may have an amount of spawns potentially around the 20 to 40 range. Each of which, need to be placed in a unique position, then each sorting Order to be established in relation to their z position to each other. I don't want a sprite to be spawned closer to the camera with a lower sorting order than a sprite that spawns further from the camera, but I do need the positions to be random. Uuuuhg - Such confoos! :-\

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dameon_ · Apr 30, 2014 at 01:53 AM

You could maintain an array with previously used z positions and compare them all to the current random number to see if any previous random numbers were the same. You just set up a loop that will run as long as the randomly generated number is the same.

Generally, setting up a loop where you don't know a concrete end is coming is bad policy, but the odds are insanely against generating the same random number enough to create a loop that repeats multiple times.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity c# indexoutofrangeexception: array index is out of range 1 Answer

Best way to keep track of objects on a 3D Grid? 2 Answers

How to debug values in jagged arrays? 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