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 SkOrtt · Jun 26, 2018 at 09:56 AM · positionobjectsifcheckspawnpoints

Checking position

HI! Im new here, I need some advices, I have 1 spawner which spawn monsterns random on 9 spawnpoints at random time and my question is, How to check whether an object is created on a spawnpoint and if it is to stop creating another object on a spawnpoint.

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 tormentoarmagedoom · Jun 26, 2018 at 10:05 AM 0
Share

Good day. Can you provide the code you are using? or you need to know how to spawn them?

avatar image SkOrtt tormentoarmagedoom · Jun 26, 2018 at 10:08 AM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Gnom$$anonymous$$enager : $$anonymous$$onoBehaviour {

 public GameObject spawnThing;
 public float spawnWait;
 public float spawn$$anonymous$$ostWait;
 public float spawnLeastWait;
 public int startWait;
 public bool stop;
 public int endPoints;

 public Transform[] spawnPoints;
 public GameObject[] monsters;
 int randomSpawnPoint, random$$anonymous$$onster;
 public static bool spawnAllowed;

 int randEnemy;



  void Start()

 {
     StartCoroutine("SpawnObject");
  

  }

void Update() { spawnWait = Random.Range(spawnLeastWait, spawn$$anonymous$$ostWait);

 }


 IEnumerator SpawnObject(){
     
     yield return new WaitForSeconds(startWait);
     while (Points.coin !=endPoints){
         randomSpawnPoint = Random.Range(0, spawnPoints.Length);
         random$$anonymous$$onster = Random.Range(0, monsters.Length);
         Instantiate(monsters[random$$anonymous$$onster], spawnPoints[randomSpawnPoint].position,
              Quaternion.identity);
         yield return new WaitForSeconds(spawnWait);
     }


  
 }

 void GameOver(){
     if(Points.coin >= endPoints){
         Debug.Log("End");
     }
 }

   




}

avatar image SkOrtt tormentoarmagedoom · Jun 26, 2018 at 10:11 AM 0
Share

i want that if there is an object at a given point, so that it will not spawn twice

2 Replies

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

Answer by ModLunar · Jun 26, 2018 at 12:05 PM

A simple solution you could use is - create an array of booleans, the same size as your array of Transform[] of spawn points, so that each boolean in the boolean array corresponds to one of the spawn points, and keeps track of whether or not you spawned something at that particular spawn point.

You'd just need to make sure you set the booleans at the appropriate times to keep the booleans up to date (weird things will go wrong if you don't keep your variables up to date ;) )

Another possibility is to make a private (non-serialized, aka not shown in the inspector) List once your game starts. You can fill this list with all the spawn points initially. Then, you can pick one of the spawn points remaining in that list, and remove it once you use it. That way, what's left in the List is only the possible spawn points left over to be used up.

Wow I didn't think I'd be rambling on more! That List solution I thought of could actually be used with a Queue as well, if you put the spawn points into the Queue in a random order.

Anyway, the variables you would use would look something like this for the possible solutions I brought up:

 //--- --- --- --- --- Boolean array solution:
 //...
 
 public Transform[] spawnPoints;
 private bool[] alreadySpawned;
 
 public void Awake() {
     alreadySpawned = new bool[spawnPoints.Length];
     //Nothing more to do, the boolean array initializes everything
     //to default values (false for each boolean)
 }
 
 /*
     When spawning from a random spawnPoint, you'll need to keep generating a new random index
     and checking if you already spawned there until you finally generate a random index that hasn'that
     been used up already.
 */
 
 //--- --- --- --- --- List of Transforms solution:
 //...
 
 public Transform[] spawnPoints;
 private List<Transform> spawnPointList; //List<T> is defined in System.Collections.Generic
 
 public void Awake() {
     spawnPointList = new List<Transform>(spawnPoints.Length);
     for (int i = 0; i < spawnPoints.Length; i++)
         spawnPointList.Add(spawnPoints[i]);
 }
 
 /*
     You can then access spawnPointList with an indexer just like an array (ex: spawnPointList[i])
     Each time you access something, you can then use spawnPointList.RemoveAt(i), where i is the 0-based integer index.
 */
 
 //--- --- --- --- --- Queue solution:
 //...
 
 public Transform[] spawnPoints;
 private Queue<Transform> spawnPointQueue; //Queue<T> is also in System.Collections.Generic
 
 public void Awake() {
     List<int> alreadyAdded = new List<int>(spawnPoints.Length);
     //alreadyAdded is empty here, it just has the internal capacity to grow up to spawnPoints.Length
     //without needing a (relatively) expensive resize operation.
     
     //Create the Queue and add to it the spawn points in a random order :D
     spawnPointQueue = new Queue<Transform>(spawnPoints.Length);
     for (int i = 0; i < spawnPoints.Length; i++) {
         int newIndex = Random.Range(0, spawnPoints.Length);
         //Gotta make sure this new (randomly generated) index was not already added to the Queue
         while (alreadyAdded.Contains(newIndex))
             newIndex = Random.Range(0, spawnPoints.Length);
         
         spawnPointQueue.Enqueue(spawnPoints[newIndex]);
     }
 }
 
 /*
     You can preview the first element in the Queue with spawnPointQueue.Peek(). This means you don't remove it from the Queue.
     But when spawning, you'll probably want to take a spawn point and remove it from the Queue -- in which case,
     you can make use of spawnPointQueue.Dequeue(), which returns the first element and removes it from the Queue!
 */
 
 
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
avatar image
1

Answer by tormentoarmagedoom · Jun 26, 2018 at 11:45 AM

Good day.

I did this in one of my projects to be sure all random instantiated objects was at least 500 units away from others. (they was Spawn points)

I created my own detector. Just an EmptyObject with a collider and a script to detect all spawnpoints in a 500 units distance. Then sends a message to the Instantiating script if another spawnpoint was detected in a 500 units range. Then autodestroy itselfs.

So, if no "detection message" was received, is because there is no other spawnpoints there, so i can continue with the instantiate of the real object.

If another spawn was already there, just re-calculate the random point and commence again

:D

BYE!

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

106 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

Related Questions

Check if two objects have the same position. 2 Answers

Position of Instantiated Objects from a reference 1 Answer

Problems with spawnpoints 1 Answer

i want to check play position in the game so i can take an action 1 Answer

Deactivate/activate objects depending on player position 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