Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by darkmon54 · Jan 26 at 07:37 PM · triggerreferencinghealth

how to call a method from another script using trigger?

hey! i have this playerHealth script where everything from setting up the UI, healthvalues etc. are put in, it also has a heal method where when a player collides with a game object that has the tag healthOrb it would increase the healthvalue by 1

 public class playerHealth : MonoBehaviour
 {
   public int maxHealth = 5;
   public int currentHealth;
 
   public healthBarController healthbar;
     // Start is called before the first frame update
   void Start()
   {
     currentHealth = maxHealth;
     healthbar.SetMaxHealth(maxHealth);
   }
 
   public void OnCollisionEnter2D (Collision2D other)
   {
     if(other.collider.tag == "monster")
     {
       TakeDamage(1);
     }
   }
 
   public void healHealth(int Heal)
   {
     currentHealth += Heal;
     healthbar.SetHealth(currentHealth);
   }
 
   public void TakeDamage(int damage)
   {
     currentHealth -= damage;
     healthbar.SetHealth(currentHealth);
   }
 
 }
 

the only concern i have with this is refrencing the healhealth method in another script and calling it through trigger

 public class trigger : MonoBehaviour
 {
   public playerHealth playerhealth;
   public GameObject breakParticle;
   public GameObject healthparticle;
   public cameraShake Shake;
 
   void start()
   {
     Shake = GameObject.FindGameObjectWithTag("screenShake").GetComponent<cameraShake>();
   }
 
   public void OnTriggerEnter2D (Collider2D other)
   {
     if(other.GetComponent<Collider2D>().tag == "player")
     {
       Shake.camshake();
       Destroy (gameObject);
       Instantiate (breakParticle, transform.position, Quaternion.identity);
     }
     if(other.GetComponent<Collider2D>().tag == "monster")
     {
       Shake.camshake();
       Destroy (gameObject);
       Instantiate (breakParticle, transform.position, Quaternion.identity);
     }
     if(other.GetComponent<Collider2D>().tag == "healthOrb")
     {
       //this doesnt work btw, i think because it didnt register even when no error was shown on the 
       console
       playerhealth.healHealth(1);

       Destroy (gameObject);
       Instantiate (healthparticle, transform.position, Quaternion.identity);
     }
   }
 }
 

sorry if my script looks sht im stll learning as of now(・-・;)

Comment
Add comment · Show 2
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 kylecat6330 · Jan 27 at 08:46 AM 0
Share

What are your scripts attached to?

avatar image darkmon54 kylecat6330 · Jan 27 at 09:03 AM 0
Share

the playerhealth is attached to the player and the trigger script is attached to the gameobject (healthOrb) with trigger on that will trigger the particle and heal method

1 Reply

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

Answer by kylecat6330 · Jan 27 at 09:09 AM

Just some general things I saw that may help overall. First of all you shouldn't use other.GetComponent().tag on all your if statements you should be able to just do other.tag instead because other is already the Collider2D. If for whatever reason you can't use other.tag simply set it to a variable at the top like "string tag = other.GetComponent().tag" and then use tag in your if statements.

Now as for your problem it may be better to use a "playerHealth = other.GetComponent()" in OnTriggerEnter under your health tag rather than declare it as a public. That will make sure that they a referencing the correct scripts which may be your problem. If that doesn't fix then add some Debug.Logs to your script to see if the functions are being called but just are not working, or if they are not being called.

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 darkmon54 · Jan 27 at 06:12 PM 0
Share

thank you very much! i will surely try this right now and also as for the if statement other.Getcomponent().tag i didnt actually write that down what i wrote was just a simple other.collider.tag but after the API update it change my script to other.GetComponent().tag

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

158 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

Related Questions

LoadLevel Health Question. 1 Answer

Health does not update in this script, why? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Health System Help 2 Answers

Variable from other class always returns 0 1 Answer


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