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 HernandoNJ · Jan 05, 2021 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, 2021 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.

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

Generate random no and active that GO using array 1 Answer

How to prevent picking the same combination in array? 1 Answer

How to fix some of location have more than 1 object? 2 Answers

unique list of 10 random numbers 4 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