How to create a list that spawns enemies that are placed into the list via the unity inspector

I am trying to create a list of enemies that can be spawned via trigger in unity. However I am running into an issue where my public GameObject enemy seen in the enemiesToSpawn class is called on lines 29 and 31 VS code returns the following error “An object reference is required for the non-static field, method, or property SpawnEnemiesTrigger.enemiesToSpawn.enemy“. I am not sure what how to solve this error and would like some suggestions on ow to fix it or different ways to achieve this.

Also I am trying use this script as much as possible through out my game so please make sure your solutions are reusable.

My Code:

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

public class SpawnEnemiesTrigger : MonoBehaviour
{
    [System.Serializable]
    public class enemiesToSpawn
    {
        public GameObject enemy;
    }

    //create the list of enemies
    public List<GameObject> enemyList = new List<GameObject>();

    public void OnTriggerEnter2D(Collider2D col)
    {
        if(col.gameObject.tag == "Player")
        {
            for(int i = 0; i < // error -> enemyList.Count; i++)
            {
                if( // error -> enemiesToSpawn.enemy != null)
                {
                    Instantiate(enemy, new Vector2(-0.35f, -44f), Quaternion.identity);
                }
                else
                {
                    Debug.Log("enemiesToSpawn is null");
                    return;
                }
                Debug.Log("instantating enemy " + i);
            }
        }
    }
}

New contributor

Thomas Wilson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Your problem is here.

[System.Serializable]
public class enemiesToSpawn
{
    public GameObject enemy;
}

This is a class, like SpawnEnemiesTrigger is a class. You are trying to use it like a variable.

It looks like you actually don’t need this class though.

public class SpawnEnemiesTrigger : MonoBehaviour
{
    // delete the class we don't need it

    // Create the list of enemies
    // You don't want to make a `new` instance here because it's serialised in the inspector. 
    //`new` would overwrite this. 
    // Consider making this `[SerializeField] private` to make this explicit.
    public List<GameObject> enemyList;

    public void OnTriggerEnter2D(Collider2D col)
    {
        if(col.gameObject.tag == "Player")
        {
            for(int i = 0; i < enemyList.Count; i++) // this could be a `foreach` loop
            {
                var enemy = enemyList[i]; // <-- use the enemy from the list!
                if(enemy != null) 
                {
                    Instantiate(enemy, new Vector2(-0.35f, -44f), Quaternion.identity);
                }
                else
                {
                    Debug.Log("enemiesToSpawn is null");
                    return;
                }
                Debug.Log("instantating enemy " + i);
            }
        }
    }
}

For your first error, instead of a for loop, you can use foreach loop and go through all the elements in the list.

The second error as @Enigmativity explain above, you are checking enemiesToSpawn as a type and not as an instance of that type.

Below code is a quick fix of your OnTriggerEnter2D method without using your enemiesToSpawn class.

public void OnTriggerEnter2D(Collider2D col)
{
    if(col.gameObject.tag == "Player")
    {
        foreach(var enemy in enemyList)
        {
            Instantiate(enemy, new Vector2(-0.35f, -44f), Quaternion.identity);
        }
    }
}

Also @derHugo is an expert on Unity, so you might wanna directly contact with him and help you out with more details

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật