Search Unity

Question Trying to get my character to gain health from a pickup

Discussion in 'Scripting' started by BenHenriques, Apr 21, 2021.

  1. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    So I want the pickup to bring my character's currentHealth up by 25 when they walk over the pickup however my character doesn't react when I do, I have a box collider on my pickup set to true with this script on it. Let my know if you need my players health script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PowerUp : MonoBehaviour
    6. {
    7.     public GameObject pickupEffect;
    8.    
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.gameObject.CompareTag("Player"))
    12.         {
    13.             Pickup(other);
    14.         }
    15.     }
    16.  
    17.     void Pickup(Collider player)
    18.     {
    19.         Instantiate(pickupEffect, transform.position, transform.rotation);
    20.        
    21.         PlayerHealth stats = player.GetComponent<PlayerHealth>();
    22.         stats.currentHealth += 25;
    23.  
    24.         Destroy(gameObject);
    25.     }
    26. }
    27.  
     
  2. Are you sure on of them (and only one of them) has the "Is Trigger" activated and has a RigidBody?
     
  3. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    I didnt have the Rigidbody on it but after I put it on nothing happened
     
  4. Does you player have the "Player" tag?
     
  5. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
  6. If you put this in your code replacing your previous similar method:
    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         Debug.Log("Collisoin with player 1");
    4.         if (other.gameObject.CompareTag("Player"))
    5.         {
    6.             Debug.Log("Collisoin with player 2");
    7.             Pickup(other);
    8.         }
    9.     }
    10.  
    What can you see in the console?
     
  7. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Both objects must have collider of same type e.g. 2d

    Player (Rigid body 2d + box collider 2d)
    Your pickup trigger (box collider 2d)


     
  8. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    After putting the OnTriggerEnter2D on my player movement script and I got the message in my console however now I am not sure how to make the object give me 25 more health. When I change the debuglog for this:
    Code (CSharp):
    1. PlayerHealth stats = player.GetComponent<PlayerHealth>();
    2.         stats.currentHealth += 25;
    I get an error since player is not referenced.
     
  9. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    because your player object has type collider and not the player gameobject.

    you should change the parameter type of "Pickup" method:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.CompareTag("Player"))
    4.         {
    5.             Pickup(other.gameObject);
    6.         }
    7.     }
    8.     void Pickup(GameObject player)
    9.     {
    10.         Instantiate(pickupEffect, transform.position, transform.rotation);
    11.      
    12.         PlayerHealth stats = player.GetComponent<PlayerHealth>();
    13.         stats.currentHealth += 25;
    14.         Destroy(gameObject);
    15.     }
    If you don´t want to change the param type, just edit the line where you get the "PlayerHealth" script:

    Code (CSharp):
    1. PlayerHealth stats = player.gameObject.GetComponent<PlayerHealth>();
     
  10. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    This is my player movement script here and at the bottom I got the trigger, I tried the changes above in my PowerUp script and I tried replacing the debug trigger with a reference to the PowerUp script with no luck, sorry i'm pretty new to this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public Character2DController controller;
    8.     public Animator animator;
    9.  
    10.     public float runSpeed = 40f;
    11.  
    12.     float horizontalMove = 0f;
    13.     bool jump = false;
    14.     bool crouch = false;
    15.  
    16.     void Update () {
    17.  
    18.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    19.  
    20.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    21.  
    22.         if (Input.GetButtonDown("Jump"))
    23.         {
    24.             jump = true;
    25.             animator.SetBool("IsJumping", true);
    26.         }else if (Input.GetButtonUp("Jump"))
    27.         {
    28.             jump = false;
    29.         }
    30.  
    31.         if (Input.GetButtonDown("Crouch"))
    32.         {
    33.             crouch = true;
    34.         } else if (Input.GetButtonUp("Crouch"))
    35.         {
    36.             crouch = false;
    37.         }
    38.  
    39.         if (Input.GetKeyDown(KeyCode.Mouse1))
    40.         {
    41.             animator.SetTrigger("IsThrowing");
    42.         }
    43.  
    44.     }
    45.  
    46.     public void OnLanding ()
    47.     {
    48.         animator.SetBool("IsJumping", false);
    49.     }
    50.  
    51.     public void OnCrouching (bool isCrouching)
    52.     {
    53.         animator.SetBool("IsCrouching", isCrouching);
    54.     }
    55.  
    56.     void FixedUpdate ()
    57.     {
    58.         // Move our character
    59.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    60.         jump = false;
    61.     }
    62.  
    63.     private void OnTriggerEnter2D(Collider2D collider)
    64.     {
    65.         Debug.Log("Trigger!");
    66.     }
    67. }
    68.  
    69.  
     
  11. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Most simple way in my opinion is something like this :
    1)Create static class with player health
    2)Just add health in static class
    Code (CSharp):
    1.  
    2. private void OnTriggerEnter2D(Collider2D collider)
    3.     {
    4.       staticClass.playerHealth += 25;  
    5.     }
    6.  
    3) In case you want do display current health on screen
    4)Create game object with text and in script what handle your ui just set


    Code (CSharp):
    1.  
    2. using.UnityEngine.UI;
    3.  
    4.  public class MenuUI : MonoBehaviour
    5.     {
    6.  
    7. void Update()
    8. {
    9. gameObject.GetComponent<Text>().text = staticClass.playerHealth.ToString()
    10. }
    11.  
    12. }
    13.  
     
    Last edited: Apr 23, 2021
  12. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Btw you can use one trigger method for all objects you can just set tag to each object for example :
    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collider)
    2. {
    3.         if(collider.gameObject.tag == "healthPotion")
    4. {
    5.   staticClass.playerHealth += 25;
    6. }
    7.         if(collider.gameObject.tag == "deathPotion")
    8. {
    9.   staticClass.playerHealth -= 25;
    10. }
    11.  
    12.      
    13.     }
     
  13. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    128
    At my site i have working scripts all about health. Perhaps it can help you out.

     
  14. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Hello, sorry for the very late reply, metixgosu I tried :
    Code (CSharp):
    1.  
    2. private void OnTriggerEnter2D(Collider2D collider)
    3.     {
    4.       staticClass.playerHealth += 25;
    5.     }
    6.  
    And I got the error:
    The name 'staticClass' does not exist in the current context
    Do I just have to reference it at the top of my script? If so how would I do this?
     
  15. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You dont need any reference in your script since it is a static class just create static class and enter playeHealth variable inside.

    Code (CSharp):
    1. static class StaticClass
    2.     {
    3. static int playerHealth = 0;
    4.  
    5. }
     
    Last edited: May 2, 2021
  16. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Most of the errors went away except: PowerUp.StaticClass.playerHealth': cannot declare instance members in a static class
    This is my PowerUp script now after the changes, I could also post my PlayerHealth script too if needed, sorry for the trouble im lost here
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PowerUp : MonoBehaviour
    6. {
    7.     public GameObject pickupEffect;
    8.     static class StaticClass
    9.     {
    10.         int playerHealth = 0;
    11.     }
    12.     private void OnTriggerEnter(Collider collider)
    13.     {
    14.         if (collider.gameObject.tag == "Health")
    15.         {
    16.             staticClass.playerHealth += 25;
    17.         }
    18.     }
    19.  
    20.     void Pickup(GameObject player)
    21.     {
    22.         Instantiate(pickupEffect, transform.position, transform.rotation);
    23.  
    24.         PlayerHealth stats = player.gameObject.GetComponent<PlayerHealth>();
    25.         stats.currentHealth += 25;
    26.  
    27.         Destroy(gameObject);
    28.     }
    29. }
    30.  
     
  17. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    should be static int , did you watch some beginner tutorial about c# i wrote it wrong but if you will post to forum with every single error you never code anything just use google and you have anwswer in 10second
     
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You can actually edit your example post code so that it isn't incorrect for the next 256 people who come through over the next decade, copy / paste it and then get confused. :)
     
    vargata and Realspawn1 like this.
  19. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Done :) i hope you help me in my new "help wanted" topic!! :)