- Home /
Respawn Error
Hello people! I am having a slight problem with my game, at the moment I have enemies that spawn and move towards the player. Once the player dies, I have coded them to stop moving towards the player and to stop spawning (or so I think I have)
The problem is when I click the "Reload" button that I have implemented, which basically just loads the scene back up "Application.LoadLevel"
I get this error, "NullReferenceException: Object reference not set to an instance of an object EnemyMove.Update () (at Assets/Scripts/EnemyMove.cs:14)"
The game runs fine though even after the error, any ideas on what I am doing wrong?
Here is the code for that particular script...
using UnityEngine;
using System.Collections;
public class EnemyMove : MonoBehaviour {
float speed = 3.0f; // move speed
public static float score = 0.0f;
public GameObject Explosion;
void Update(){
if (GameObject.FindGameObjectsWithTag ("Player") != null && PointAndFire.Hit >= 1) {
GameObject target = GameObject.FindGameObjectWithTag ("Player");
transform.position = Vector3.MoveTowards (transform.position, target.transform.position, speed * Time.deltaTime);
} else if (GameObject.FindGameObjectsWithTag ("Player") == null) {
}
{
}
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "bullet 2(Clone)")
{
Destroy(col.gameObject);
score += 1.0f;
Debug.Log(score);
Instantiate (Explosion,transform.position, transform.rotation);
}
}
void OnCollisionPlayer (Collision col)
{
if(col.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
}
I think the error lies in void Update
Try seperating pointandfire.hit from if statement and put it in another if statement under player check.
(unrelated) why are you using GameObject.FindGameObjectsWithTag? (GameObjects) - needs to be GameObject(at the else statement)
also, i don't use C# so i don't know if this is actually relevant but you are asking if it's null:
if (GameObject.FindGameObjectsWithTag ("Player") == null)
would saying:
if (!GameObject.FindGameObjectWithTag ("Player"))
work?
Sorry guys, none of the above worked :( still getting the same error message
Answer by wijesijp · Apr 04, 2014 at 08:29 AM
I think you should use
GameObject.FindGameObjectWithTag
not
GameObject.FindGameObjectsWithTag
in your if statement
Your answer
Follow this Question
Related Questions
Stupid "Reference Exception Error" 1 Answer
Despawner script returns null reference exception. 0 Answers
Help with error: UnassignedReferenceException 5 Answers
NullReferenceException Error 6 Answers