Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 $$anonymous$$ · Oct 10, 2018 at 04:56 PM · playerenemydamagehealth

[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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Oct 11, 2018 at 04:13 PM 0
Share

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

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

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

My enemy doesn't die when my player attacks, when I try to click space bar, the player is hitting the enemy, but the enemy only acknowledges the damage after five or more hits, any advice? 1 Answer

OverslapSphere dont detect enemyhealth 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