Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 latsushi · Oct 29, 2018 at 01:45 AM · gameobjectinstantiaterandomarraysarray of gameobjects

Three Spots For Three Random Objects

I've three spots in a row at the top of the screen (represented by the 'slideSpots' array.) My goal is to press play and then have three objects (astronaut, rocket, satellite) from a list (represented by the 'objects' array) instantiate in each of those three spots. So, I want an astronaut in one of the spots. I want a rocket in another one of the spots, and I want the satellite in the last available spot. What's happening currently when I press play is that three objects instantiate, but often I get repeats (two astronauts for example) and I've more than one object instantiated on one spot (overlap.)

Here's my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RandomItems : MonoBehaviour {
 
     public GameObject[] slideSpots;
     public GameObject[] objects;
     public GameObject theSpot;
 
     public void Start()
     {
         SpawnSlideSpots();
         SpawnSlideSpots();
         SpawnSlideSpots();
     }
     void SpawnSlideSpots()
     {
         int spawn = Random.Range(0, slideSpots.Length);
         int stuff = Random.Range(0, objects.Length);
 
         theSpot = GameObject.Instantiate(objects[stuff], slideSpots[spawn].transform.position, Quaternion.identity);
     }
 }
 
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 GamitusLabs · Oct 29, 2018 at 03:05 AM

The reason is that you are never verifying that you've used the randomly selected object. If you are randomly selecting one (object or spot) there's no real reason to randomize the other.

 for ( i = 1 - 3)  // loop through your 3 spots
     {
       object = random object

       if ( i > 1 )
       {
          while(array[1] == object || array[2] == object)  //if the object is already used, try another
          {
             object = random object
          }
          array[i] = object
       }
       else
          array[1] = object
     }

It should be obvious, but this is pseudo-code. And, if you decide to up your spot count, you'll have to modify the while loop. Additionally, it could be more optimized to check what's been used but this would do what you need it to.

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 latsushi · Oct 30, 2018 at 01:05 AM 0
Share

I'm getting the same problem. @GamitusLabs Here's my current code now:

 public GameObject[] slideSpots;
     public GameObject[] objects;
     GameObject theSpot;
 
     GameObject obj;
 
     int i;
 
     public void Start()
     {
         SpawnSlideSpots();
         SpawnSlideSpots();
         SpawnSlideSpots();
     }
 
     void SpawnSlideSpots()
     {
         int spawn = Random.Range(0, slideSpots.Length);
         int stuff = Random.Range(0, objects.Length);
 
         theSpot = GameObject.Instantiate(objects[stuff], slideSpots[spawn].transform.position, Quaternion.identity);
 
         for (i = 0; i >= 3; i++)
         {
             theSpot = objects[stuff];
             i++;
             if (i > 0)
             {
                 while (slideSpots[1] == theSpot || slideSpots[2] == theSpot || slideSpots[3] == theSpot)
                 {
                     theSpot = objects[stuff];
                     Debug.Log(i);
                 }
                 slideSpots[i] = theSpot;
             }
             else
                 slideSpots[1] = theSpot;
         }
     }
 }

What am I missing?

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

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

C# 2d Instantiate object randomly around gameobject 1 Answer

Find one inactive player (gameobject) 2 Answers

Instantiate from array into array? 2 Answers

Keep random Instantiation from spawning ontop of other objects 1 Answer

Randomizing a selection from an array 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