#Need help with unity error...how to fix

12 messages · Page 1 of 1 (latest)

deep stump
#

THIS IS MY WORLD ENEMY SPAWNER SCRIPT (this stores every spawner and manages enemy spawning)

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

public class WorldEnemySpawner : MonoBehaviour
{
public List<LoneEnemySpawner> spawnerQueue = new List<LoneEnemySpawner>();
public LoneEnemySpawner[] spawners;
public GameObject[] enemies;

public static WorldEnemySpawner instance;
private void Awake()
{
    if (instance == null)
        instance = this;
    else
        Destroy(gameObject);

    spawners = GetComponentsInChildren<LoneEnemySpawner>();
}

private void Start()
{
    foreach (LoneEnemySpawner spawner in spawners)
    {
        spawner.SpawnEnemy(enemies[0]);
    }
}

private void Update()
{
    List<LoneEnemySpawner> spawnerQueueCopy = new List<LoneEnemySpawner>(spawnerQueue);

    foreach (LoneEnemySpawner spawner in spawnerQueueCopy)
    {
        spawner.SpawnEnemy(enemies[0]);
        spawnerQueue.Remove(spawner); // Modify the original list
    }
}

}

#

THIS IS THE LONE SPAWNER SCRIPT (the actual spawners)

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

public class LoneEnemySpawner : MonoBehaviour
{

public GameObject spawnedEnemy;
public bool spawnerOccupied = true;
public bool requestedReplacement = false;

public float spawnerCooldown = 3f;
// Start is called before the first frame update

void Start()
{
    Debug.Log(transform.position);
}

// Update is called once per frame
void Update()
{
    if (spawnedEnemy == null && requestedReplacement == false)
    {
        StartCoroutine(RequestEnemyReplacement(spawnerCooldown));
        spawnerOccupied = false;
    }
}

public void SpawnEnemy(GameObject enemy)
{
    requestedReplacement = false;
    spawnerOccupied = true;

    
    spawnedEnemy = Instantiate(enemy, transform.position, transform.rotation);
}

public IEnumerator RequestEnemyReplacement(float time)
{
    requestedReplacement = true;
    yield return new WaitForSecondsRealtime(time);
    WorldEnemySpawner.instance.spawnerQueue.Add(this);
}

}

#

^THIS IS THE OBJECT I AM TRYING TO SPAWN

#

^this is the error

#

for some reason, even though this error pops up, the game works fine

#

however, this error always pops up when i instantiate it via script

swift groveBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ``` (3 backticks. Click here to see where the key is)

To use syntax highlighting, add the file extension of the language you wish to highlight (cs for C#, cpp for C++)

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

foggy turtle
#

Youre likely using the wrong method overload or something when spawning

#

Its warning you that when you spawn the object its outside the scene more or less

#

You should use codeblocks for code though