- Home /
MissingReferenceException:
So I'm doing Brackys how to make a TD Game Here's the video link
here's my code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyMovement : MonoBehaviour
{
public float speed = 1f;
private Transform target;
private int wavepointIndex = 0;
void Start()
{
target = Waypoints.waypoints[0];
}
void Update()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
if(Vector3.Distance(transform.position, target.position) <= .2f)
{
getNextWaypoint();
}
}
void getNextWaypoint()
{
if(wavepointIndex >= Waypoints.waypoints.Length - 1)
{
Destroy(gameObject);
return;
}
wavepointIndex++;
target = Waypoints.waypoints[wavepointIndex];
}
}
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object
I don't know what's wrong with it. and here is the whole error.
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at :0) UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0) UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0) WaveSpawner.SpawnEnemy () (at Assets/WaveSpawner.cs:54) WaveSpawner+d__7.MoveNext () (at Assets/WaveSpawner.cs:44) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at :0)
Then here's the code that's it's pointing to
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour
{
public Transform enemyPrefab;
public Transform spawnPoint;
public float timeBetweenWaves = 5f;
private float countdown = 2f;
private int waveIndex = 0;
public Text waveCountdownText;
void Update()
{
if(countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
}
countdown -= Time.deltaTime;
waveCountdownText.text = Mathf.Floor(countdown).ToString();
}
IEnumerator SpawnWave()
{
waveIndex++;
for (int i = 0; i < waveIndex; i++)
{
SpawnEnemy();
yield return new WaitForSeconds(.5f);
}
}
void SpawnEnemy()
{
Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
}
}
Heading
Never$$anonymous$$d I got it. Thank you for that idea.
For the enemy prefab - i was plugging in a prefab in the hierarchy. not the one from the project folder prefab.
so i deleted the one from the hierarchy. then plugged the enemy prefab to the wave spawner and it is now working without errors.
Answer by karlojeda · Aug 11, 2020 at 04:47 AM
Solved:
For the enemy prefab - i was plugging in a prefab in the hierarchy. not the one from the project folder prefab.
so i deleted the one from the hierarchy. then plugged the enemy prefab to the wave spawner and it is now working without errors.
Answer by IllegalRazer · Aug 12, 2020 at 09:22 AM
Im guessing that u assigned a gameObject with the Enemy movement script to the spawnPoint variable in the waveSpawner.
However, in the EnemyMovevemt Script, the script destroys the gameObject the script is linked to in line 37. This causes the reference to the gameObject of spawnPoint to be null. In waveSpawner, you tried to access the position and rotation of spawnPoint. However, the gameObject assigned to spawnPoint has been destroyed. This causes your script to try to access null, causing the error.
It is very unusual for a script to destroy its linked gameObject, so I would suggest modifying line 37 in EnemyMovement.,Im guessing that u assigned a gameObject with the Enemy movement script to the spawnPoint variable in the waveSpawner.
However, in the EnemyMovevemt Script, the script destroys the gameObject the script is linked to in line 37. This causes the reference to the gameObject of spawnPoint to be null. In waveSpawner, you tried to access the position and rotation of spawnPoint. However, the gameObject assigned to spawnPoint has been destroyed. This causes your script to try to access null, causing the error.
It is very unusual for a script to destroy its linked gameObject, so I would suggest modifying line 37 in EnemyMovement.
Answer by IllegalRazer · Aug 11, 2020 at 04:45 AM
Im guessing that u assigned a gameObject with the Enemy movement script to the spawnPoint variable in the waveSpawner.
However, in the EnemyMovevemt Script, the script destroys the gameObject the script is linked to in line 37. This causes the reference to the gameObject of spawnPoint to be null. In waveSpawner, you tried to access the position and rotation of spawnPoint. However, the gameObject assigned to spawnPoint has been destroyed. This causes your script to try to access null, causing the error.
It is very unusual for a script to destroy its linked gameObject, so I would suggest modifying line 37 in EnemyMovement.
Your answer
Follow this Question
Related Questions
MissingReferenceException Help 1 Answer
DontDestroyOnLoad error 2 Answers
MissingReferenceException Help Please! 0 Answers
Why Player input aways get MissingReferenceException? 0 Answers