• 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 yoda8789 · Jan 24, 2018 at 07:51 PM · beginnerhorror

How to activate random gameobjects from a list

I'm a beginner at this and i'm doing horror game where you have to collect coins to win. I spread 33 coins around the map in different locations and I want to activate only 8 of them chosen randomly at start so they aren't always in the same spots. And when the game ends and start over the coins are again in random spots. So if you have a solution to my problem please help :)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by callen · Jan 24, 2018 at 08:35 PM

bhavinbhai2707 was on the right track, but here is the bug-free version.

 // You need to have an array of all your coins. you could do it with a public variable, and drag them into the inspector...
 public GameObject[] coins;
 
 void Start() {
     // ...or, you could also less-efficiently get the coins here with something like:
     // var coins = FindObjectsOfType<Coin>();
 
     for(int i=0; i<8; i++)
     {
         int rnd = Random.Range(0, coins.Length);
         if(!coins[rnd].activeSelf) coins[rnd].SetActive(true);
         else i--; // repeat loop iteration to try and find a disabled coin
     }
 }

The main issue with the other answer is that it cannot guarantee you will always activate 8 coins. You could, in a rare case, activate just 1, or any number from 1 to 8. My version should work, assuming you have more than 8 coins in the array (otherwise it will infinite loop!)

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
avatar image
0

Answer by bhavinbhai2707 · Jan 24, 2018 at 08:23 PM

  1. one of the way is to spread coins and place them manually around map(deactivated manually by default) and store them in array and activate randomly

      public GameObject[] coins;         //array of coins from scene view
     
        private void Start()
       {
           spawnRandom();
       }
         private void spawnRandom()
         {
             int last = 0;                   
             for (int i = 0; i < 8; i++)    //i < 8 since u want 8 coins
             {
                 int num = Random.Range(0, 20);           //Random.Range fuction throws random number
                 while (num == last)                         //checking if new number is not equal to last number
                     num = Random.Range(0, 20);          //if yes then reassign num so the same coin wont reactivate
                 coins[num].SetActive(true)
                 last = num;
             }
         }
    
  2. second way is you can spawn prefab of coin Randomly around map

     public GameObject coinprefab;         //array of coins from scene view
         private float xlimit, zlimit;         //basically the limit of the boundary
     
     private void Start()
     {
          spawnRandom();
     }
         private void spawnRandom()
         {
            for(int i = 0; i < 8; i++)
             {
                 xlimit = Random.Range(-xlimit, xlimit);            //random x position
                 zlimit = Random.Range(-zlimit, zlimit);            //random z position 
                Instantiate(coinprefab, new Vector3(xlimit, 3f, zlimit), Quaternion.identity);         //spawning the coin in scene & set y of vector3 according to your need
             }
         }
    
    

These codes are untested. I hope these helps :)

Comment
Add comment · Show 2 · 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 callen · Jan 24, 2018 at 09:12 PM 0
Share

The second answer would not work unless the map was empty - otherwise coins will spawn inside of walls or other inaccessible places. Also the OP said he already placed his coins on the map, presumably in places that will be more fun than completely-random ones would.

So, The first answer is better conceptually, but you need to fix the following issues:

  1. OP said there were 33 coins, but you only do Random.Range(0, 20) which will make the last 13 coins unusable. You should just ins$$anonymous$$d do Random.Range(0, coins.Length);

  2. You are trying to use "last" to prevent the same coin from being chosen multiple times, but what you coded doesn't do that. For example, your first Random returns 9, and last is 9, so you call Random.Range again... but what if you get a 9 again? Your code doesn't handle this. The same can be said for the case where you get 9, then on next loop you get 5, then on next loop you get 9 again - at this point last==5, so it will not detect the duplicate.

  3. gameObject.SetActive(coins[num]) is incorrect, should be coins[num].SetActive(true)

Take note of my answer, as it solves all of these issues with just a few lines of code.

avatar image bhavinbhai2707 callen · Jan 24, 2018 at 09:29 PM 0
Share

thanx for correcting mate :), i knew there are some bugs and as i wrote the code itself on this websites editor. i will take note of all the issue thanx :)

avatar image
0

Answer by yoda8789 · Jan 24, 2018 at 09:38 PM

Thanks to both of you! Couldnt have done that without your help :) @bhavinbhai2707 @callen

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

83 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

Related Questions

Basic skills required to create a first person game like "Slender"? 3 Answers

In order to call GetTransformInfoExecectUpToDate 1 Answer

I want to make an undertale kinda game, how do I implement pixl art? 1 Answer

Changing text values every time you instantiate the button 1 Answer

Can't display my Countdown timer text on my end menu. (newbie needing help) 1 Answer

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