• 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 HernandoNJ · Jan 05 at 03:54 AM · arraylistrandomrandom.range

Selection list from Array Unity - Random - GameObjects array

After creating a GameObjects array (checkpoints), I want to take just some of its values to set them as valid checkpoints, with no repeated values. I was looking for an answer in internet but I didn't got an easy to understand solution.

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 HernandoNJ · Jan 05 at 03:59 AM

 using System.Collections.Generic;
 using UnityEngine;
 
 public class CheckpointsManager : MonoBehaviour
 {
     private GameObject[] checkpoints;
 
     private List<int> numbersList1;
     private List<int> numbersList2;
 
     void Start()
     {
         // Create an array with all GOs with tag "Checkpoint"
         checkpoints = GameObject.FindGameObjectsWithTag("Checkpoint");
         SetValidCheckpoint();
     }
 
     private void SetValidCheckpoint()
     {
         // create Lists objects
         numbersList1 = new List<int>();
         numbersList2 = new List<int>(); //list with selected numbers
 
         // Add numbers to numbersList1 using GOs array.Length (checkpoints.Length)
         // checkpoints.Length is the number of total checkpoints
         for (int i = 0; i < checkpoints.Length; i++)
             numbersList1.Add(i);
 
         // *** print numbersList1 values, just for checking them out...
         // foreach (int num in numbersList1)
         // Debug.Log("numberslist1: " + num);
 
         // *for loop used to add numbers to numbersList2 and checkout values
         // numbersList2 keeps the valid checkpoints values
         // if the random value is repeated (if it's already contained in numbersList2)
         // it is not added, and the loop counter is not increased
 
         int n = 4; // number of valid (desired) checkpoints
         for (int i = 0; i < n;) // i++ is applied only if the number is not repeated
         {
             int randNum = Random.Range(0, numbersList1.Count);
 
             // just for checking in console
             Debug.Log("choosen number: " + randNum);
 
             if (!numbersList2.Contains(randNum)) // if the list2 doesn't contain the randNum
             {
                 numbersList2.Add(randNum); // needed because we need to check out if number is repeated
                 checkpoints[randNum].tag = "ValidCheckpoint"; // change GO tag
                 i++; // increase the for loop counter
             }
             else // if the number is repeated
                 Debug.Log("number " + randNum + "is already included in numbersList2");
 
             // just for checking out in console
             Debug.Log("n2 list count: " + numbersList2.Count);
 
             // just for checking out in console
             foreach (int num in numbersList2)
                 Debug.Log("+++ numbers in numberslist2: " + num);
 
         }
     }
 }


Code without comments and others

 using System.Collections.Generic;
 using UnityEngine;
 
 public class CheckpointsManager : MonoBehaviour
 {
     private GameObject[] checkpoints;
 
     private List<int> numbersList1;
     private List<int> numbersList2;
 
     void Start()
     {
         checkpoints = GameObject.FindGameObjectsWithTag("Checkpoint");
         SetValidCheckpoint();
     }
 
     private void SetValidCheckpoint()
     {
         numbersList1 = new List<int>();
         numbersList2 = new List<int>(); 
 
         for (int i = 0; i < checkpoints.Length; i++)
             numbersList1.Add(i);
 
         int n = 4; 
         for (int i = 0; i < n;) 
         {
             int randNum = Random.Range(0, numbersList1.Count);
 
             if (!numbersList2.Contains(randNum)) 
             {
                 numbersList2.Add(randNum); 
                 checkpoints[randNum].tag = "ValidCheckpoint"; 
                 i++; 
             }
         }
     }
 }

Now you have 4 not repeated random numbers that can be used as index for a list or array of valid checkpoints, and instantiating as well. You can copy/paste in VS for better visualization.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

134 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 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

Creating a record of values generated with Random.Range 1 Answer

Randomly remove list values 1 Answer

Make Random.range to not repeat numbers 3 Answers

Randomising list of integers and removing them from list once the task assigned to the integer in the project is completed 0 Answers

How to know what random number is chosen 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges