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 basavaraj_guled · Mar 04, 2015 at 09:24 AM · positionspawnobjectsoverlap

overlapping of gameobjects position

hi there, i have script that has 2 public array variables called spawnPoints and spawnObjects, i have 9 spawnPoints and 4 spawnObjects. These 4 spawnObjects are placed on 4 spawnPoints. Here what i am doing is whenever i press the mouse button i m changing the position of spawnObjects to other spawnPoints, its working fine but the spawnObjects are overlapping some times.

 using UnityEngine;
 using System.Collections;
 
 public class Forward : MonoBehaviour
 {
         public GameObject[] spawnPoint;
         private GameObject spawn;
         public GameObject[] spawnObjects;
         private GameObject objectSpawn;
 
         void Update ()
         {
                 if (Input.GetMouseButtonDown (0)) {
                         
                         int a = Random.Range (0, spawnPoint.Length);
                         int b = Random.Range (0, spawnObjects.Length);
                         spawn = spawnPoint [a];
                         objectSpawn = spawnObjects [b];
                         objectSpawn.transform.position = spawn.transform.position;
                 }
         }
 }

alt text

capture.png (11.6 kB)
Comment
Add comment · Show 4
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 Adam-Buckner ♦♦ · Mar 04, 2015 at 09:24 AM 0
Share

So, you state that you have overlapping spawn points...

... But you don't mention the issue you are having with this.

I'm assu$$anonymous$$g that the problem is you don't want you spawn objects to overlap?

Let us know.

avatar image hexagonius · Mar 04, 2015 at 09:53 AM 1
Share

Create a copy of your spawnPoints. Whenever you picked a position for a spawnObject remove the chosen position from the copy. This is easily done by having a list ins$$anonymous$$d of an array. Loop through the array to adding each entry to that list to fill it. Call RemoveAt to remove the current index you found.

Check out the gridPositions list in this tutorial for reference

avatar image basavaraj_guled · Mar 09, 2015 at 05:21 AM 0
Share

@$$anonymous$$Buckner: ya exactly i want to do that

avatar image sandeepsmartest · Mar 09, 2015 at 05:46 AM 0
Share

Or the better way is to create an array which contains the spawn position and make a list that contains numbers(unique random numbers) nothing but index of the spawn postion array.

Untested code but should work. Thank you Nsk

 //$$anonymous$$ethod required to create random numbers 
     IEnumerable<int> UniqueRandom(int $$anonymous$$Inclusive, int maxInclusive)
     {
         List<int> candidates = new List<int>();
         for (int i = $$anonymous$$Inclusive; i <= maxInclusive; i++)
         {
             candidates.Add(i);
         }
         System.Random rnd = new System.Random();
         while (candidates.Count > 0)
         {
             int index = rnd.Next(candidates.Count);
             yield return candidates[index];
             candidates.RemoveAt(index);
         }
     }
     void PositionSpawnObjects()//method to position the spawn objects randomly WRT to Desired positions already stored in spawnPositions array
     {
         for(int i=0;i<SpawnPositionArr.Length;i++)
         {
             spawnObjectsArr[i].transform.position = spawnPositions[RandomNumbersList[i]];//spawnPositions-array consits of desired position for da objects to spawn
         }
     }
     void Update () {
         
 
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
         {
             foreach (int i in UniqueRandom(0,4)) //gives random numbers from 0 to 4 and adds those numbers to an list
             {
                 print(i);
                 RandomNumbersList.add(i);//this list contains numbers without duplicated
             }   
             PositionSpawnObjects();
         }
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to increment value of axis when something happens 2 Answers

Best way to spawn new objects that arent already in the scene? 1 Answer

NullReferenceException: Instatiate with construtor OR in Inspector 1 Answer

AR Object stuck in front of camera 0 Answers

Why does this script spawn 13 ClickEffect prefabs instead of 1? 2 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