Search Unity

Need Help with 2D collision Detection.

Discussion in 'Scripting' started by JustaDuck97, Dec 18, 2019.

  1. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    I'm trying to create a simple health system by using an integer that is lowered every collision then, have the GameManager check the int and if it is 0 or under reset the scene. My trouble comes from detecting any collision to begin with. I've never done a health system before, and in my game any collision on the player will be damage.

    Code (CSharp):
    1. void OnCollisonEnter2D(Collision collision)
    2.     {
    3.         if (collision.gameObject.name == "FollowEnemy")
    4.         {
    5.  
    6.             HitPoint -= Damage;
    7.             Debug.Log("Damaged");
    8.         }
    9.     }
    I know that the Object name is correct.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  3. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    Yup, it's always a spelling mistake that i'm always overlooking.
    Thanks
     
  4. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    It’s not free, but Rider editor will prevent these mistakes as it knows all the Unity callback functions and their signatures.
    Or simply be sure to triple check spelling and arguments with the docs when ever typing callback functions.
     
  5. Deleted User

    Deleted User

    Guest

    So does Visual Studio. It write all the Unity functions automatically. The typo in the script line the op posted should not have happened in the op knew how to use Visual Studio.
     
  6. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    Speaking of, anyone knows why unity didn’t go for OOP virtual methods model instead of magic functions?
     
  7. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    Never presume? Ironic.
     
  8. Deleted User

    Deleted User

    Guest

    Just stating the obvious.
     
  9. Deleted User

    Deleted User

    Guest