[solved] Collision damage and Health Controller dont work
Hey everone, Im just a beginner with unity and programming. Here are the scripts which arn't working:
HealthController:
public float startHealth = 5;
public int startLifePoints = 3;
private float health = 5;
private int lifePoints = 3;
private Animator anim;
private PlayerController playerController;
private bool isDead = false;
private bool isDamageable = true;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
playerController = GetComponent<PlayerController>();
//der levelindex muss dem spiel entsprechend angepasst werden, wenn es eine begrüssungsszene oder eun hauptmenue gibt.
if (Application.loadedLevel == 0)
{
health = startHealth;
lifePoints = startLifePoints;
}
health = PlayerPrefs.GetFloat("Health");
lifePoints = PlayerPrefs.GetInt("LifePoints");
}
void ApplyDamage (float damage)
{
if (isDamageable)
{
health -= damage;
health = Mathf.Max(0, health);
if (!isDead)
{
if (health == 0)
{
isDead = true;
Dying();
}
else
{
if (isDamageable)
{
Damaging();
}
}
isDamageable = false;
Invoke("ResetIsDamageable", 1);
}
}
}
void ResetIsDamageable()
{
isDamageable = true;
}
void Dying ()
{
anim.SetBool("Dying", true);
playerController.enabled = false;
lifePoints --;
if (lifePoints <= 0)
{
//start game
Invoke("StartGame", 3);
}
else
{
// restart level
Invoke("RestartLevel", 1);
}
}
void StartGame()
{
Application.LoadLevel(0);
}
void RestartLevel()
{
health = startHealth;
anim.SetBool("Dying", false);
playerController.enabled = true;
if (!playerController.lookingRight)
{
playerController.Flip();
}
// level neu generieren und spieler zurück setzen
}
void Damaging ()
{
anim.SetTrigger("Damage");
}
void OnDestroy()
{
PlayerPrefs.SetFloat("Health", health);
PlayerPrefs.SetInt("LifePoints", lifePoints);
}
CollisionDamage:
public float damage = 1;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
other.SendMessage("ApplyDamage", damage);
}
}
void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
other.SendMessage("ApplyDamage", damage);
}
}
}
The problem is, that the CollisionDamage-script cant use the ApplyDamage of the HealthController. this is the error message: SendMessage ApplyDamage has no receiver! UnityEngine.Component:SendMessage(String, Object) CollisionDamage:OnTriggerStay2D(Collider2D) (at Assets/Scripts/CollisionDamage.cs:20)
(these scripts are from youtube (Hummelwalker) and it works in his tutorial... :( ) Thank you for your answer! :) <3
Answer by Trevdevs · May 18, 2016 at 03:29 PM
Add this to the end of the SendMessage by default it looks for a receiver.
other.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiever);
Would help if i could spell, lol Send$$anonymous$$essageOptions.DontRequireReceiver);
Thank you very much for the quick answer! There is no error anymore, but the player dont get hurt, and no healthpoints are removed... :(
The possiblities i see to that is 1. The Player gameobject does not have the Tag "Player" on it. 2. Or health is resetting itself to its max value somewhere in your functions.
Otherwise try placing some Debug.Log's is areas where the health is updated or reset.
Try this Debug.Log here to detect a health change
void ApplyDamage (float damage)
{
if (isDamageable)
{
health -= damage;
health = $$anonymous$$athf.$$anonymous$$ax(0, health);
Debug.Log(health);
Now I added Debug.Log("Hit") in the collider-script (below other.Send$$anonymous$$essage("ApplyDamage", damage); ), and the message appeared in the console, but the debug.Log("Health"); in the health controller didn't appear in the console. I also dont think that the health are resettet somewhere. But thaks again! :)
Answer by WbrJr · Jun 15, 2016 at 07:43 PM
I HAVE THE ANSWER!!! I just have to put the colliders of the Char on the Object with the health controller. I'm verry happy that I know what the problem was, but now I have a other Problem: My Char has normal long leggs, but when he pulls his legs to himself, the Collider is still in the normal position in the upper Object. My brain is so destroyd in the momend...
Your answer
Follow this Question
Related Questions
unexpected void error 1 Answer
Object reference not set to an instance of an object with raycast2d 0 Answers
CS0117 Error 0 Answers
My game script is fine but I'm still getting unexpected class error 0 Answers