Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by WbrJr · May 18, 2016 at 02:54 PM · c#unity 2derror messagecontrollercolliderhit

[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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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);

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trevdevs · May 18, 2016 at 03:20 PM 0
Share

Would help if i could spell, lol Send$$anonymous$$essageOptions.DontRequireReceiver);

avatar image WbrJr · May 18, 2016 at 08:47 PM 0
Share

Thank you very much for the quick answer! There is no error anymore, but the player dont get hurt, and no healthpoints are removed... :(

avatar image Trevdevs WbrJr · May 18, 2016 at 09:16 PM 0
Share

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);
 





avatar image WbrJr Trevdevs · May 19, 2016 at 06:18 AM 0
Share

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! :)

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

switch between 5 Ui icons by pressing a button 0 Answers

How do I make a scrollbar for the camera? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges