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
1
Question by dmfx9 · Apr 21, 2020 at 03:01 PM · instantiatearray of gameobjectsspawnpoints

how to use multiple spawn points

I'm trying to spawn enemies from 4 different spawn points I have the 4 spawn points set as empty game objects which are children of the spawn manager game object. I have found some code to put them in an array but I cant seem to get the vector3 position from the array to use when I instantiate an enemy. This is the code I am trying to use any help would be much apricated as I'm still learning everything.

 public class SpawnManager : MonoBehaviour
 {
     [SerializeField]
     private GameObject enemyPrefab;
     private int spawnIndex;
     private Transform[] spawnpoints;
     private Vector3 spawnPos;
     
     // Start is called before the first frame update
     void Start()
     {
         spawnpoints = GetComponentsInChildren(typeof(Transform)) as Transform[];
         spawnIndex = Random.Range(0, spawnpoints.Length);
         
         InvokeRepeating("spawnEnemys", 1, 5);
     }
 
    
     
     void spawnEnemys()
     {
         Instantiate(enemyPrefab, spawnpoints[spawnIndex], enemyPrefab.transform.rotation);
     }
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 yummy81 · Apr 21, 2020 at 04:21 PM

GetComponentsInChildren will give you not only transforms of the spawn points but also transform of the SpawnManager. So it is better to fill "spawnpoints" array manually:

     count = transform.childCount;
     spawnpoints = new Transform[count];
     for(int i = 0; i < count; i++){
         spawnpoints[i] = transform.GetChild(i);
     }

Next, if you assign spawnIndex in the Start method you will do it only once. All the time enemies will be instantiated in the same spawn point, which is not what you want. So assignment of the spawnIndex should be done in the "spawnEnemys" method. It will guarantee that enemies will appear each time in different place. Here's the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnManager : MonoBehaviour
 {
     [SerializeField]
     private GameObject enemyPrefab;
     private int spawnIndex;
     private Transform[] spawnpoints;
     private Vector3 spawnPos;
     private int count;
     
     void Start(){
         count = transform.childCount;
         spawnpoints = new Transform[count];
         for(int i = 0; i < count; i++){
             spawnpoints[i] = transform.GetChild(i);
         }
         
         InvokeRepeating("spawnEnemys", 1, 5);
     }
     
     void spawnEnemys(){
         spawnIndex = Random.Range(0, count);
         
         Instantiate(enemyPrefab, spawnpoints[spawnIndex].position, enemyPrefab.transform.rotation);
     }
 }


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

154 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

Related Questions

Instantiate Problem 2 Answers

Problem with object generation script? 1 Answer

Optimizing a massive array of GameObjects pls help 1 Answer

How do I control where enemies spawn? 1 Answer

How do I spawn an object under another moving object at random in 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