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 Chappy · Nov 03, 2013 at 10:17 PM · c#variablehealth

Sending a variable to a script, then returning it to the previous script. C#

I'm currently working on enemy design for a game and I need to get it to damage the player. I need to send the variable from the player health script, to the enemy script, have the enemy script alter the health value and then send it back to the health script. The problem is, I have no real clue how to do this.

Very basic health script (Ignore the commented out parts, that's something for another question.)

 public class PlayerHealth_Test : MonoBehaviour {
     
 public static int CurrentHealth = 100;
 
     
     //void Update () {
     
 //    if (CurrentHealth == 0)
     //    {
             //Destroy(GameObject(Player.Object));    
     //    }
     //
     //}
 }


And the enemy script:

 public class Leech_AI_Test : MonoBehaviour {
     
 public float MaxDistance = 10.0f;
 public float MoveSpeed = 5.0f;
 public float MinDistance = 0.0f;
 public Transform Target;
 public float LookAtDistance = 10.0f;
 public float Distance;
 public float Damping = 5.0f;
 private Quaternion rotation;
 public bool parenting = false;
 public float curHealth;
 
 
 void Start (){
 
     Target = GameObject.FindGameObjectWithTag ("Player").transform;
     parenting = false;
     
    // curHealth = gameObject.GetComponent<PlayerHealth_Test>().CurrentHealth;
     
 }
 
 
 void OnTriggerEnter(Collider Player)
     {
         transform.parent = GameObject.FindGameObjectWithTag("Player").transform;        
             parenting = true;    
         }
     
 void Update () {
 
     Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
         
     if (Distance < LookAtDistance)
     {
         LookAt();
     }
         
     if (Distance > MinDistance && Distance < MaxDistance && parenting == false)
     {
         follow ();
     }
         
     
     //Debug.Log("curHealth", curHealth);
         
 }
 
 void FixedUpdate ()
     {
         if (parenting == true)
         {
             curHealth -= 0.05f;
         }    
     }
     
 void LookAt(){
 
     rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
     
 }
     
 void follow(){
         
     transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
         
         
 }
     
     
 }


I need to send the currenthealth value from the playerhealth script to the leech script, allow the leech script to damage the player, and send that value back to the playerhealth script, and that needs to happen continuously until the leech is dealt with.

Any information on how to do this, or a better way to do this (since this seems pretty unrefined to me) would be appreciated.

Comment
Add comment · Show 1
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 Tomer-Barkan · Nov 03, 2013 at 10:42 PM 0
Share

This tutorial here could help:

http://www.youtube.com/watch?v=Xrp8fiNVWvg

1 Reply

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

Answer by aldonaletto · Nov 03, 2013 at 10:38 PM

Just get a reference to the PlayerHealth_Test script and modify CurrentHealth directly. If this script is attached to the player, use Target to get the script:

 ...
 public bool parenting = false;
 public float curHealth;
 private PlayerHealth_Test playerHealth; // declare this variable
  
 void Start (){
     Target = GameObject.FindGameObjectWithTag("Player").transform;
     // get a reference to PlayerHealth_Test that's attached to the player:
     playerHealth = Target.GetComponent<PlayerHealth_Test>();
     parenting = false;
 }
  
 ...
  
 void FixedUpdate (){
     if (parenting){
         // modify CurrentHealth directly in the target script:
         playerHealth.CurrentHealth -= 0.05f;
     } 
 }

 ...
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 Chappy · Nov 03, 2013 at 10:50 PM 0
Share

That did it, thank you very much.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[SOLVED] How do I change the values of an HDRP Volume Profile at Runtime? 1 Answer

how do you access and use a variable from a separate script then run an if statement depending on the value of that variable? 0 Answers

(C#) Enemy health and take damage from bullets 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