A million dollar question!!!

Hello everybody!
I will be very grateful if anyone can help me!
I use AdvancedTutorial package for bilding my game.
Instead of bullets, my players shoot arrows and I did the code that calls the ApplyDamage method, when an arrow hits the player

void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Player") {

var serializer = col.GetComponent();

if (serializer != null) {
col.GetComponent ().ApplyDamage (22);
}
}

And it works well, but when the player has little HP or HP == 0 I get an error

//error
NullReferenceException: Object reference not set to an instance of an object
Bolt.AdvancedTutorial.PlayerController.ApplyDamage (Byte damage) (at Assets/samples/AdvancedTutorial/scripts/Player/PlayerController.cs:228)
GetHp.OnTriggerEnter (UnityEngine.Collider col) (at Assets/samples/AdvancedTutorial/scripts/MyScripts/Arrow/GetHp.cs:27)



//ApllyDamage Method
public void ApplyDamage (byte damage)
{
if (!state.Dead) {

state.health -= damage;

if (state.health > 100 || state.health < 0) {
state.health = 0;
}
}

if (state.health == 0) {
//Error indicates this line, but I do not understand the reason ????
entity.controller.GetPlayer ().Kill ();
}
}


Tell me please, what's wrong with my code?

Comments

  • Sorry for the trouble, something in another reason, I'm missing Entity.controller will now look for what's wrong
  • And why there can be a null error for the entity.controller.GetPlayer () .Kill ();?
Sign In or Register to comment.