NullReferenceException: Object reference not set to an instance of an object PlayerController.Die ()

hi Teacher and EveryOne
i can’t next level Win Screen
i got problem signal : NullReferenceException: Object reference not set to an instance of an object
PlayerController.Die () (at Assets/Entities/my Player/PlayerController.cs:75)
PlayerController.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/Entities/my Player/PlayerController.cs:67)
this my source : https://drive.google.com/open?id=1NZeDWXGmDp2g_5ZzHd80BGIgPSRF3yhq
please help me !!!

using UnityEngine;
using System.Collections;`Preformatted text`

public class PlayerController : MonoBehaviour {
	public float speed = 20.0f;
	public float padding = 1f;          // luu y 01
	public GameObject projectile;// sau dok loi laser vao no va nhan apply
	public float projectileSpeed ;
	public float firingRate = 10f;
	public float health = 800f;
	// Use this for initialization
	float xmin ;
	float xmax ;
	// ma~ cu~ la` float xmin=-5 ; va2 xmax =5;
	//giup phi thuyen ra ben ngoai con phan nua hinh`
	void Start(){

		float distance = transform.position.z - Camera.main.transform.position.z;
		Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
		Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance));
		xmin = leftmost.x + padding; // luu y 01
		xmax = rightmost.x - padding;// luu y 01
	}
	
	// Update is called once per frame
	void Fire(){
		Vector3 offset = new Vector3 (0, 0.6f, 0);
		GameObject beam = Instantiate (projectile, transform.position + offset, Quaternion.identity)as GameObject;
		beam.rigidbody2D.velocity = new Vector3(0,projectileSpeed,0);
	}
	void Update () {
		if (Input.GetKeyDown (KeyCode.Space)) {
			InvokeRepeating ("Fire", 0.000001f, firingRate);
		}
// luu y : chu~ F bat buoc phai viet hoa
		if (Input.GetKeyUp (KeyCode.Space)) {
			CancelInvoke ("Fire");
		}

//dong 27 se lam cho beam laser ban len tren
			// nhung phai trong Hierachy click Player> cho 5 trong projectileSpeed cua Inspector
// va` prefab player laser > add rigidbody2D
		//}
		//line 7 va 8 + line 23 den 25 >>> khi nhan phim Space laser se xuat hien ngay vi tri ship vua dung dok
		if (Input.GetKey (KeyCode.LeftArrow)) {
			//transform.position += new Vector3  (-speed *Time.deltaTime , 0, 0);
			transform.position += Vector3.left * speed *Time.deltaTime ;
		}else if(Input.GetKey (KeyCode.RightArrow)) {
			//transform.position += new Vector3 (speed *Time.deltaTime , 0, 0);
			transform.position += Vector3.right * speed *Time.deltaTime ;
			//deltatime giup phi thuyen di chuyen muot hon
			// luu y " speed " va " -speed "
		}
		float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
		transform.position = new Vector3 (newX, transform.position.y, transform.position.z);
		//clamp se giup phi thuyen gioi han trong khoang x va -x = 5
	}
	void OnTriggerEnter2D(Collider2D collider){

		Projectile missile = collider.gameObject.GetComponent<Projectile> ();
		//luu y : GetComponent ko co chu s o cuoi chu~; collider ko viet hoa dau` chu~
		if (missile) {
			Debug.Log ("player da va cham voi missile");
			health -= missile.GetDamage();
			missile.Hit();
			if (health <=0){
				Die();
				Debug.Log ("chet tui roi !!!");
			}
//line 61 den line 67 + chon kiem is trigger trong poly colider cua player,enemy se kill dc player
		}
	}
	void Die(){
		Destroy(gameObject);
		LevelManager man = GameObject.Find ("LevelManager").GetComponent<LevelManager> ();
		man.LoadLevel("Win Screen");
//luu y : GetComponent ko co chu~ s phia sau
	}
}

Hi there,

you don’t have a LevelManager in your Game scene :wink:

3 Likes

Also Hiha, please see the link below, thanks :slight_smile:


See also;

1 Like

oh thankyou Sebastian_Martens and Rob very much
i gotting that problem
and both 2 you helped me
thankyou very much much much !!! :kissing_heart::kissing_heart::kissing_heart:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms