Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Maryoomi · Jul 04, 2015 at 12:40 PM · c#2d gameinstantiationrandomspawningx axis

How to Spawn an Object in between Randomly Spawned Objects with no Overlapping?

Hello,

I am trying to spawn multiple clones of one object and make its position between a number of randomly spawned objects. In other words, let's say we have many clones of the square objects in different colors, and they are all randomly spawned in one line without overlapping. The square objects are spawned perfectly for me, and what I need to do additionally is to spawn two clones of a circle object and position the circle clones in between the square objects and without overlapping. As displayed in the picture below:

alt text

This is the script I am using to spawn the random squares, and I also added a part where I tried to spawn the circles but it didn't work with me as the circles totally overlapped with the squares.

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;         //Allows us to use Lists.
 using Random = UnityEngine.Random;         //Tells Random to use the Unity Engine random number generator.
 
 namespace Completed
     
 {
     
     public class BoardManager : MonoBehaviour
     {
         
         // Using Serializable allows us to embed a class with sub properties in the inspector.
         [Serializable]
         public class Count
         {
             public int minimum;             //Minimum value for our Count class.
             public int maximum;             //Maximum value for our Count class.
             
 
             //Assignment constructor.
             public Count (int min, int max)
             {
                 minimum = min;
                 maximum = max;
             }
         }
         
         
         public int columns = 9; 
         public Count redSquareCount = new Count (1, 2); //Lower and upper limit for our random number of squares per level.
         public Count greenSquareCount = new Count (1, 1);
         public Count purpleSquareCount = new Count (1, 1);
         public Count creamCupboardCount = new Count (1, 1);
         public Count circleCount = new Count (2, 2);
         public GameObject[] redSquares;    //Array of floor prefabs.
         public GameObject[] greenSquares;
         public GameObject[] purpleSquares;
         public GameObject[] circles;
         
                             
         private List <Vector3> gridPositions = new List <Vector3> ();
         
 
         void InitialiseList ()
         {
             //Clear our list gridPositions.
             gridPositions.Clear ();
             
             //Loop through x axis (columns).
             for(int x = 2; x < columns; x++)
             {
 
                 //At each index add a new Vector3 to our list with the x coordinate of that position.
                 gridPositions.Add (new Vector3(x, 0.3f, 0f));
             }    
 
         }
         
 
         //RandomPosition returns a random position from our list gridPositions.
         Vector3 RandomPosition ()
         {
             //Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
             int randomIndex = Random.Range (0, gridPositions.Count);
             
             //Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
             Vector3 randomPosition = gridPositions[randomIndex];
             
             
             //Remove the entry at randomIndex from the list so that it can't be re-used.
             gridPositions.RemoveAt (randomIndex);
             
             
             //Return the randomly selected Vector3 position.
             return randomPosition;    
         }
 
         
         //LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
         void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
         {
             //Choose a random number of objects to instantiate within the minimum and maximum limits
             int objectCount = Random.Range (minimum, maximum+1);
             
             //Instantiate objects until the randomly chosen limit objectCount is reached
             for(int i = 0; i < objectCount; i++)
             {
                 
                 //Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
                 Vector3 randomPosition = RandomPosition();
                 
             
 
                 //Choose a random tile from tileArray and assign it to tileChoice
                 GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
                 Vector3 size = tileChoice.GetComponent<Renderer>().bounds.size;
 
                 randomPosition += new Vector3(randomPosition.x * size.x, randomPosition.y * size.y, randomPosition.z * size.z);
 
                 
                 //Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
                 Instantiate(tileChoice, randomPosition, Quaternion.identity);
 
             }    
 
         }
 
         
         
         //SetupScene initializes our level and calls the previous functions to lay out the game board
         public void SetupScene (int level)
         {
 
             //Reset our list of gridpositions.
             InitialiseList ();
             
             //Instantiate a random number of object tiles based on minimum and maximum, at randomized positions.
             LayoutObjectAtRandom (redSquares, redSquareCount.minimum, redSquareCount.maximum);
             LayoutObjectAtRandom (greenSquares, greenSquareCount.minimum, greenSquareCount.maximum);
             LayoutObjectAtRandom (purpleSquares, purpleSquareCount.minimum, purpleSquareCount.maximum);
             LayoutObjectAtRandom (circles, circleCount.minimum, circleCount.maximum);
           
         }
 
 
     }
 }
 


squarecircles.png (16.0 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2d spawn across screen 1 Answer

Can't get game objects to instantiate at random positions without overlap - please help! 3 Answers

How to Spawn and Place an Object in Line after Spawned Randomized Object? 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