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 JoePeirsonGames · Jan 13, 2020 at 09:38 AM · gameobjectarrayspawngameobjectsspawning

Checking if a position is occupied in 2D?

I am wanting to only spawn something at a location IF there is nothing already there. I am new and cant work out how to do it. This is what i have so far.

Spawn Manager:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnManager : MonoBehaviour
 {
 
     [Header("Arrays")]
     public Transform[] spawnPoints;
     public GameObject[] spawnPointsObjects;
     public GameObject temp;
 
     [Header("Other Variables")]
     public GameObject thing;
     int randomSpawnPoint;
     public static bool spawnAllowed;
 
     void Start()
     {
         spawnAllowed = true;
         InvokeRepeating("SpawnThing", 0f, 1f);
     }
 
     void SpawnThing()
     {
         if (spawnAllowed)
         {
             randomSpawnPoint = Random.Range(0, spawnPoints.Length);
             Instantiate(thing, spawnPoints[randomSpawnPoint].position, Quaternion.identity);
             Debug.Log("Spawned at " + spawnPoints[randomSpawnPoint]);
         }
     }
 }

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 unity_ek98vnTRplGj8Q · Jan 13, 2020 at 06:18 PM

The answer to this will vary depending on exactly what you are trying to do and how exactly your game works. Looking at how you are doing it, there are 2 answers that seem plausible.


The general solution would just be to check if there is anything that exists in a radius around the spawn point. This is easy to do with Physics2D.OverlapCircle https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html. You can simply put all of your things on to the same layer, and then use OverlapCircle at the spawn point with that layer mask and see if it returns a hit.


Depending on how your game works, this solution could be fine. But lets say you have 100 spawn points, and every time you call the spawn function you always want something to be spawned somewhere. Checking random points with OverlapCircle again and again until you find an open spot can get very expensive. A more efficient way to do it would be to directly mark the spawn points as occupied. You already have a list of your spawn points, so all you need is a dictionary that will tell you if a spawn point is occupied. Dictionary<Transform, bool> spawnOccupied;. Every time you spawn something somewhere, you can just mark the spawn position as occupied spawnOccupied[spawnPoint] = true;, and then have some mechanism that will mark the spawn point as unoccupied once the enemy moves away. This will be more complicated than the first solution, but may be more efficient.

Comment
Add comment · 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

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

198 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 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

Spawn an object with additional parameter 1 Answer

Multiple spawn points using Array... 1 Answer

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer

How Do I Add An Instantiated Object To An Array? 3 Answers

How to spawn only one GameObject from a list of game objects? Please help. 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