Hello everyone. Hope this finds you all well.
I am new to scripting, so please be gentle.
Most of what I have achieved has been done watching a few short tutorials online and then just self experimenting with the knowledge achieved…
However, I have reached a problem I can’t seem to either locate on google/forum searches, or experimentation.
The result I am looking to get is random Y spawns for enemies from this spawner. I have some variables already setup (Note the public floats and random floats in IEnumerator…
but however I try and achieve this, they either don’t spawn at all, or I get errors galore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner1 : MonoBehaviour
{
[SerializeField]
private GameObject[] monsterReference;
[SerializeField] private Transform rightPos;
public float maxX;
public float maxY;
public float minX;
public float minY;
private GameObject spawnedEnemy;
private int randomIndex;
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy()
{
float X = Random.Range(minX, maxY);
float Y = Random.Range(minY, maxX);
while (true)
{
yield return new WaitForSeconds(Random.Range(2, 5));
randomIndex = Random.Range(0, monsterReference.Length);
spawnedEnemy = Instantiate(monsterReference[randomIndex]);
spawnedEnemy.transform.position =
spawnedEnemy.transform.position = rightPos.position;
Destroy(spawnedEnemy, 8f);
}
}
Sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
How to fix a NullReferenceException error
Three steps to success:
Identify what is null ← any other action taken before this step is WASTED TIME
Identify why it is null
Fix that
NullReference is the single most common error while programming. Fixing it is always the same.
For other errors:
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
the description of the error itself (google this; you are NEVER the first one!)
the file it occurred in (critical!)
the line number and character position (the two numbers in parentheses)
also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.