[Solved] I can't call Method from Referenced Script
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?
Answer by JVene · Oct 10, 2018 at 08:09 PM
PlayerHurt is a method in the PlayerHealthManagement class. To call it you must use an instance of that class. You have an instance declared _playerHealth. If that is set properly, the call is made with:
_playerHealth.PlayerHurt( damage );
In this situation, _playerHealth identifies the unique instance upon which to call PlayerHurt (so it could be one of many players), and the type of _playerHealth informs the compiler where to find the method to call (the class it is defined in). The combination of instance and class type (PlayerHealthManagement in this case) is required to make a function call on any method in "another" class.
Aha, I had successfully Referenced my Script. I just forgot, how to correctly Call a $$anonymous$$ethod from a Referenced Script.
I changed the Title of this Question from ,,I can't Refence Script"to ,,[Solved] I can't call $$anonymous$$ethod from Referenced Script",
and of Course, I approved your Answer.
Your answer
Follow this Question
Related Questions
How can I get my enemy to hurt/kill player 0 Answers
Enemy Isn't Hurting Player in Survival Shooter 0 Answers
Why is my player not getting more damaged? 0 Answers
OverslapSphere dont detect enemyhealth 0 Answers