Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to get a list of all of a specific game object?

Discussion in 'Scripting' started by jolechallinor3, Mar 10, 2019.

  1. jolechallinor3

    jolechallinor3

    Joined:
    Mar 6, 2019
    Posts:
    9
    Hello!

    First off, I apologize if this is a stupid question. I'm a pretty big beginner when it comes to Unity and C# specifically. If you can, try to make your explanations in a way that's not too hard for me to understand. Again, I apologize for this. :)

    So I'm currently working on a game similar to the likes of Killing Floor, with waves of enemies to fight. It's going well, but I'm having trouble figuring out part of the wave system. There are numerous invisible spawn objects scattered randomly across the map, and I'm struggling to figure out how to get a list of these objects, so one can randomly be picked and an enemy can be spawned at its position.

    Does anyone know a way to help? Thanks!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @jolechallinor3

    Well it is a broad question, but if you google "unity get objects" you will get the most common methods in top hits. You will also get a list of keywords which you can use to study the topic even more.
     
    jolechallinor3 likes this.
  3. pod11

    pod11

    Joined:
    Jan 6, 2019
    Posts:
    60
    You could make one spawn manager to manage spawns.
    in its code create public array of game objects.
    in inspector drag spawn points into slots of that array.
    for picking random point use "random.range"
    use instantiate in spawn manager code to spawn things in location of gameobject with randomly chosen array index.

    Or alternatively you could :
    in spawn manager create public game object variables.
    in inspector drag all spawn points into slots created on spawn manager.
    randomize number of new variable with random.range(min,max);
    make several if statements that would spawn something in position depending on randomized variable.
    like:
    if(pickedNumber=1)
    {instantiate(spawnedObject, pickedSpawnPoint.transform.position , pickedSpawnpoint.transform.rotation );}
     
    jolechallinor3 likes this.
  4. jolechallinor3

    jolechallinor3

    Joined:
    Mar 6, 2019
    Posts:
    9
    Sorry, I didn't mean for it to come off as broad, hmmm...
    I'm not sure how to explain it better. I'm simply looking to store a list of every instance of this spawner object, so the wave script can pick a random one of them from the list so the enemy can be spawned at its position.

    That's what I'm doing!
    I just realise the post is explained pretty badly (that's one thing I'm bad at aha). The spawner gameobjects are completely empty and invisible, they're just there for an easy way to get x/y/z positions to spawn the enemies. I'm looking for a way to get a list of these objects, so one can be picked randomly by the spawn/wave manager script, so it can then spawn an enemy at its position.
     
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Make a public list of vectors or transforms and drag drop them in the inspector or write a script that generates your vector3 for spawning, but as i guess your spawnpoints are handmade, do a drag and drop on a manager that sits inside your level prefab or so and call this manager and its array from your spawner for example.
     
    jolechallinor3 likes this.
  6. pod11

    pod11

    Joined:
    Jan 6, 2019
    Posts:
    60
    Im also learning so i made what you wanted for my own learning:
    range of random numbers for me is 1-7 becouse i had 7 objects( and now i realize it should be 0-6)
    Condition in update to call function in my case is just randomnumber<=, but you can do what you need.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.    
    8.  
    9.     public GameObject[] spawnPoints;
    10.  
    11.     public int randomNumber;
    12.  
    13.     public GameObject spawnee;
    14.    
    15.  
    16.     private void Awake()
    17.     {
    18.        
    19.     }
    20.  
    21.     void Start()
    22.     {
    23.        
    24.  
    25.     }
    26.  
    27.  
    28.     void Update()
    29.  
    30.     {
    31.         randomNumber = Random.Range(1, 7);
    32.         if (randomNumber<=1)
    33.         {
    34.             SpawningFunction();
    35.         }
    36.     }
    37.  
    38.     void SpawningFunction()
    39.     {
    40.         randomNumber = Random.Range(1, 7);
    41.         Instantiate(spawnee, spawnPoints[randomNumber].transform.position, spawnPoints[randomNumber].transform.rotation);
    42.     }
    43.  
    44. }
     
    jolechallinor3 likes this.