Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Every time I relaunch my game I Object reference not set to an instance of an object

$
0
0
My AI script always gets a error when I relaunch it or build it I have to delete the way point script on my nodes and add them back for it to work I don't know why this happens. Waypoints Script: using UnityEngine; public class Waypoints : MonoBehaviour { public static Transform[] points; void Start () { points = new Transform[transform.childCount]; for (int i = 0; i < points.Length; i++) { points[i] = transform.GetChild(i); } } } Enemy Script using System; using UnityEngine; public class Enemy : MonoBehaviour { public GameObject deathEffect; public GameObject BeingShot; public float speed = 10f; public int health = 500; public Transform enemyObj; private Transform target; private int wavepointIndex = 0; void Start () { target = Waypoints.points[0]; } void Update () { Vector3 dir = target.position - enemyObj.position; transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World); if (Vector3.Distance(enemyObj.position, target.position) <= 50f) { GetNextWaypoint(); } } public void takeDamage(int ammount) { GameObject effect = (GameObject)Instantiate(BeingShot, transform.position, Quaternion.identity); Destroy(effect, .3f); Debug.Log("Has taken damage"); health -= ammount; if (health <= 0) { death(); } } void death() { GameObject effect = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity); Destroy(effect, 5f); Destroy(gameObject); } void GetNextWaypoint() { if (wavepointIndex >= Waypoints.points.Length - 1) { endpath(); return; } wavepointIndex++; target = Waypoints.points[wavepointIndex]; } public void endpath() { Playerstats.Lives--; Debug.Log("U should work :/"); Destroy(gameObject); } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>