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);
}
}
}
}
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