So, I made 2 Scripts:
The 1st Script is the PLayers's Health Script, typed to mage tzhe Player's Health:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHealthManagement : MonoBehaviour {
//public Animator animator;
[HideInInspector] public int playerHealth = 28;
public int maxPlayerHealth = 29;
public int currentWaitTime; //Provides how much Time passed by Since the Player last got hurt. No Particular Time Unit.
public int invisibilityTime = 2; //How much Time must pass by before the Player can be hurt again. No Particular Time Unit.
// Use this for initialization
void Start () {
currentWaitTime = -3; /*initialize currentWaitTime*/
InvokeRepeating("IncreaseWaitedTime", 0f, 1f); //Always Add to the CurrentWaitTime (In Seconds)
}
// Update is called once per frame
void Update () {
//Check if the playerHealth is Less Than 1.
//If yes, Reload Current Scene (Scene Index must be typed in manually via Inspector).
if (playerHealth < 1)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
//Check if the playerHealth is greater than the Maximum.
//If yes, lower it to the Maximum.
if (playerHealth > maxPlayerHealth)
{
playerHealth = maxPlayerHealth;
}
}
public void PlayerHurt(int hurtAmount)
{
if (currentWaitTime < invisibilityTime) //If the currentWaitTime (the Time that passed by Since the Player last got hurt) is shorter than the Invisibility Time:
{ /*Nothing happens*/ }
else
{
//animator.Play ("Hit", 0); //Set Animation to Hurt
playerHealth = playerHealth - hurtAmount; //Hurt the Player by subtracting the HurtAmount from the playerHealth
currentWaitTime = 0; //Reset currentWaitTime
}
}
public void PlayerHeal()
{
playerHealth = playerHealth + 5;
Debug.Log("Your Health is " + playerHealth);
}
public void PlayerKill()
{
playerHealth = 0;
Debug.Log("You died");
}
void IncreaseWaitedTime()
{
currentWaitTime++; //Always Add to the CurrentWaitTime
}
void OnTriggerEnter2D(Collider2D other)
{
//If Player Touches it's Projectile
if (other.CompareTag ("PlayerWeapon"))
{
PlayerHurt(1); //Subtract One(1) from Health
}
}
}
The 2nd Script is attached to a Enemy instead of the Player, meant to Damage the Player if he touches the Enemies' 2d Trigger Collider:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamagingPlayerTrigger : MonoBehaviour
{
public PlayerHealthManagement _playerHealth;
public int damage;
void OnTriggerEnter2D (Collider2D other)
{
if (other.CompareTag ("Player"))
{
PlayerHurt(damage);
}
}
}
I think, that normally, I should've been able to Access the Player's Health Script, but instead, the Console Outputs me this Error Message:
Assets/Scripts/Enemies/DamagingPlayerTrigger.cs(14,13): error CS0103: The name `PlayerHurt' does not exist in the current context
Can I please have a Solution?
↧