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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Sep 13, 2015 at 03:15 PM · c#gameobjectlist

How to delete an object if there is another object of the same type at that position ?

Hi,I am doing a simple randomGeneration,my generation code works fine but it has a tendency to spawn more than 1 tile at a give position so i have made the script below to delete the gameobejects if another one of their kind is at their position,but it doesn't work it deletes non overlapping objects aswell.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DestroyOnTouch : MonoBehaviour {
     public List<GameObject> walls = new List<GameObject>();
     public List<GameObject> tiles = new List<GameObject>();
 
     void Start () 
     {
         Invoke ("Destroy",2f);
     }
 
     void Destroy()
     {
         walls.AddRange(GameObject.FindGameObjectsWithTag("Walls"));
         tiles.AddRange(GameObject.FindGameObjectsWithTag("Tile"));
 
         StartCoroutine(DestroyWalls());
         StartCoroutine(DestroyTiles());
     }
 
     IEnumerator DestroyWalls ()
     {
         yield return null;
         for(int c = 0; c < walls.Count; c++)
         {
             for(int y = 0; y < walls.Count; y++)
             {            
                 if(c > y || y > c)
                 {
                     //yield return new WaitForSeconds(0.1f);
                     if(walls[c].transform.position == walls[y].transform.position)
                     {
                         Destroy(walls[y],0.1f);
                         walls.Remove(walls[y]);
                     }
                 }
             }
         }
     }
 
     IEnumerator DestroyTiles ()
     {
         yield return null;
         for(int c = 0; c < walls.Count; c++)
         {
             for(int y = 0; y < tiles.Count; y++)
             {            
                 if(c > y || y > c)
                 {
                     //yield return new WaitForSeconds(0.1f);
                     if(tiles[c].transform.position == tiles[y].transform.position)
                     {
                         Destroy(tiles[y],0.1f);
                         walls.Remove(tiles[y]);
                     }
                 }
             }
         }
     }
 }
 
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 hexagonius · Sep 13, 2015 at 03:29 PM

Eliminating the problem where it comes from is probably the better idea. For the random generator:

Create a List of as many positions as you need, or all from which you want to pick (like a triple for loop for x, y and z). use your generator to pick a random position from the list of unique positions and delete the ones you picked already.

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Sep 13, 2015 at 03:51 PM 0
Share

That is indeed true but i am not an experienced coder how would u go about generating a 2d dungeon ? i dont need code i just need some pointers.

avatar image hexagonius · Sep 13, 2015 at 03:54 PM 0
Share

That more of a discussion than a question really. And different from this topic as well, please ask it separately. If my answer satisfied your case here, please mark it as answered.

avatar image $$anonymous$$ · Sep 13, 2015 at 04:20 PM 0
Share

Well my question didn't really get answered.

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

27 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

Related Questions

Getting objects out of a list and then comparing them 0 Answers

All GameObjects list to a GameObject? 0 Answers

How To Find GameObject From List And Add To Player List 0 Answers

What range of values need to be inserted if we need to pull a random item from the list? 0 Answers

Edit properties of certain list items,Changing properties of objects in a list (c#) 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