Null Reference for Spawning Character
I cant figure out whats wrong with this code I keep getting a null reference exception error but my player is referenced correctly maybe y'all can find what is wrong with this code.And he is dropped inside the component used to instantiate it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerSpawner : MonoBehaviour {
public enum SpawnState { Spawning, Waiting, Counting };
[System.Serializable]
public class Wave
{
public string name;
public int count;
public float rate;
}
private healthBar health;
public Wave[] waves;
private int nextWave = 0;
public int Recorder = 0;
public float timeBetweenWaves = 5f;
public float waveCountdown;
private float searchCountdown = 1f;
public GameObject[] spawnPoints;
public GameObject enemy;
public SpawnState state = SpawnState.Counting;
void Awake()
{
waveCountdown = timeBetweenWaves;
health = FindObjectOfType<healthBar>();
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (state == SpawnState.Waiting)
{
if (!EnemyIsAlive())
{
WaveCompleted();
}
else
{
return;
}
}
if (waveCountdown <= 0)
{
if (state != SpawnState.Spawning && !GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
{
StartCoroutine(SpawnWave(waves[nextWave]));
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}
bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Player") == null)
{
return false;
}
}
return true;
}
void WaveCompleted()
{
state = SpawnState.Counting;
waveCountdown = timeBetweenWaves;
if (nextWave + 1 > waves.Length - 1)
{
nextWave = 0;
print("All waves Complete");
}
else
{
nextWave++;
}
}
IEnumerator SpawnWave(Wave _wave)
{
state = SpawnState.Spawning;
for (int i = 0; i < _wave.count; i++)
{
SpawnEnemy();
Recorder= _wave.count;
yield return new WaitForSeconds(1f / _wave.rate);
}
if(_wave.count == 3)
{
_wave.count = 1;
}
state = SpawnState.Waiting;
yield break;
}
void SpawnEnemy()
{
//GameObject _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(enemy, transform.position, transform.rotation);
//Instantiate(Resources.Load("US_Ranger"), transform.position, transform.rotation);
}
}
This is where the error starts.
FindGameObjectWithTag returns only "active" gameobjects and as I understand, the player is inactive at that point of your script which unity can't find that gameobject then returns null.
You might want to get a reference of your player at the start of your script and use that variable throughout the script ins$$anonymous$$d of using GameObject.FindGameObjectWithTag("Player")
I think that where the confusion sets for me. Because it will return false. To where it will instantiate a new character to where it will find the player again. Functionally I can't find out the solution to why that line of code doesn't work.
If you want to instantiate a new character, you might want to use a prefab and get a reference of that instantiated prefab.
Your answer
Follow this Question
Related Questions
How to stop movement script on void start and resume after. 0 Answers
Client Spawning Player Objects 0 Answers
Rotation on camera not working in webGL 0 Answers