• 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 dodi · Jul 07, 2014 at 10:25 AM · gameobjecttilelocation

Best way to determine if there is a gameobject in multiple locations?

Hi all. How do I determine if there are no gameobjects in set locations?

I'm making a 2d tile based game and I want to check if there are any gameobjects in a set coordinate. I have no problems checking if there are any gameobjects in a location. I just ran a foreach for every gameobject with certain tags and do actions if they are in certain tiles. However, I don't know what to do for empty tiles.

I'm planning to make an empty gameobject with a collider then iterate it through the locations and check if there are any trigger/collision to other gameobjects. Should I got through this method or are there any simpler way to do this? Thank you in advance.

Comment
Add comment · Show 3
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 lukas_balaz · Jul 07, 2014 at 10:37 AM 0
Share

I dont understand ... why dont you run foreach for every gameobject with certain tags as you wrote ?

avatar image dodi · Jul 07, 2014 at 11:12 AM 0
Share

I have a 2d array that shows the content of each tile. It should have a value of 2 if there is a player character, 1 if there is a wall, etc.

Foreach would work if I have a gameobject in every location/tile but I don't. I want to know which locations has no gameobjects so I can set the values in the array.

After reading your comment I just thought of putting empty gameobjects with the tag "empty" on locations with no players, enemies, walls, etc. Would it be easier if I would make gameobjects with the tag "empty" for empty tiles rather than checking if there any gameobjects in set locations/tiles?

avatar image lukas_balaz · Jul 07, 2014 at 11:52 AM 0
Share

why dont you intialize array with 0, and then run foreach for every object and change number of every tile where something is? So empty tiles have value 0.

1 Reply

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

Answer by Andres-Fernandez · Jul 07, 2014 at 12:10 PM

Well, I can think of a couple of solutions.

First and easiest: just have a logical representation of your map. Being a 2d array, text file, whatever you want. Make your objects update your logical map whenever they move/change position and then you only need to check your logical map at the desired position.

Second: Create an array of tile positions on top of the tiles and send raycasts from each tile position to the tile. If collides with any object, then the tile is occupied. Else, it's free.

Comment
Add comment · Show 4 · 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 lukas_balaz · Jul 07, 2014 at 02:05 PM 0
Share

I think your first solution isnt the easiest, but it is the best certainly. Sending raycast, creating empty objects or even triggers is not good.

avatar image Andres-Fernandez · Jul 08, 2014 at 06:44 AM 0
Share

Well, first one seems the easiest to me. I would go for the 2D array (tile based games are perfect for that kind of logical representation).

For the second one you don't need to create anything, just have a Vector3 array to send the raycasts from and one layer to check for the characters. No triggers or anything. If your characters have colliders on, just send the raycast. Although it would cost a lot of time, that's for sure.

avatar image dodi · Jul 08, 2014 at 02:18 PM 1
Share

Thanks. I did the first one

avatar image lukas_balaz · Jul 08, 2014 at 08:32 PM 0
Share

yes, the first one is most effective, so it is the best for me. But you need to change the array every time when anything moves, so you can do some bugs easily. That is why I dont think it is most easy (something is easy for me if it is easy to code, but I like doing things effective)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Solved] Make GameObjects spawn at custom locations (Slender like) 2 Answers

How can i spawn a prefab at an empty game object? C# 2 Answers

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer

How to swap 2 game objects from any position on a 2d game pad? 0 Answers

spawn an object at the location of an empty gameobject 1 Answer

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