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 Takzdeveloper · Aug 07, 2014 at 05:49 PM · values

Adding the values of two functions together to display the total

Hi there,

Is it possible to add the values of two functions together in a separate function?

     public void TotalScoreLeft (int playerScoreLeft)
     {
         totalScoreLeft = playerScoreLeft;
         Debug.Log ("Score is being added left" +totalScoreLeft);
 
     }
 
     public void TotalScoreRight (int playerScoreRight)
     {
         totalScoreRight = playerScoreRight;
             Debug.Log ("Score is being added right" +totalScoreRight);
         
     }
 
     public void TotalScore (int totalScoreRight, int totalScoreLeft)
     {
         totalScore = totalScoreRight + totalScoreLeft;
         Debug.Log ("Total score is: " + totalScore);
     }

I basically want to display a GUI of TotalScores value, but the Debug.Log isn't printing the value of totalScore. How could I add the values of TotalScoreLeft and TotalScoreRight together?

Many thanks.

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 Alessio89 · Aug 07, 2014 at 07:48 PM 1
Share

You seem a bit confused. What you're doing is using two global variables to store two global variables and then add two of them together, all of this in three different functions?

If playerScoreLeft and playerScoreRight are global, then why don't you add them together in the TotalScore function?

  public void TotalScore (int totalScoreRight, int totalScoreLeft)
     {
         totalScore = playerScoreRight + playerScoreLeft;
         Debug.Log ("Total score is: " + totalScore);
     }

Try to be more clear on what you want to do (if it's not what I just said, that is) :)

avatar image Takzdeveloper · Aug 07, 2014 at 11:38 PM 0
Share

Hey, I definitely am confused at this point. I left an explanation of what i'm trying to do below.

playerScoreLeft and playerScoreRight are variables from a different script. I used sendmessage to the relevant functions in the script shown above in order to access those variables outside of their native script. It's all getting a bit confusing, I would appreciate if you could elaborate on a more simple way of carrying this out :).

Thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Aug 07, 2014 at 08:07 PM

This is also valid:

 public float TotalScoreLeft (int playerScoreLeft)
 {
     float totalScoreLeft = playerScoreLeft;
     return totalScoreLeft
 }
 
 public float TotalScoreRight (int playerScoreRight)
 {
     float totalScoreRight = playerScoreRight;
     return totalScoreRight
 }
 
 public void TotalScore (int totalScoreRight, int totalScoreLeft)
 {
     totalScore = TotalScoreRight(totalScoreRight) + TotalScoreLeft(totalScoreLeft);
 }

I assume this is pseudo code and the actual code is more complex. Otherwise there is no real value to having three methods to add together two floats.

Comment
Add comment · Show 3 · 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 Takzdeveloper · Aug 07, 2014 at 11:00 PM 0
Share

Ah how stupid of me, I was forgetting to return the values... Total Score Left and Right are components from different scripts that are being fed into this one to create a total score. They both collect points from two different colliders. Thank you very much for your help!

avatar image Takzdeveloper · Aug 07, 2014 at 11:35 PM 0
Share

Ah ok, that didn't work either. I am essentially using two colliders that when they destroy enemies, will store an int value. This value is stored in two scripts called "ColliderLeftScore" and "ColliderRightScore". I want to take both those values and add them together to create a total score in a separate script called "PlayerScore" and then display that as a GUI. Previously the Collider script was just one script that was used on both colliders, but the GUI in that script would just lay over the other one. Here is the code for one of the collider scripts:

 public class ScoringAndEnemyDestroyRight : $$anonymous$$onoBehaviour {
 
     public int playerScoreRight;
     public Transform scoreTargetRight;
     
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     public void Update () {
         PlayerScoreRight ();
     }
 
     public void OnTriggerEnter(Collider other){
         Debug.Log ("Entering collider");
 
         if (other.gameObject.tag == "Enemy") {
                 Destroy (other.gameObject);
                 playerScoreRight += 5;
 
         if  (other.gameObject.tag == "Boss") 
                 Destroy (other.gameObject);
                 playerScoreRight += 10;    
             //Debug.Log ("Right score: "+playerScoreRight);
         
         }
     }
 
     void PlayerScoreRight()
     {
         scoreTargetRight.Send$$anonymous$$essage("TotalScoreRight", playerScoreRight, Send$$anonymous$$essageOptions.DontRequireReceiver);
     }
 
 
 }

And this is the code I will use to total and display the score:

 public class PlayerHealth : $$anonymous$$onoBehaviour {
     
     public int health = 100;
     public int totalScoreLeft;
     public int totalScoreRight;
     public int totalScore;
     
     
     public void ApplyDamage (int damage)
     {
         health -= damage;
         Debug.Log ("Health is being reduced");
         if (health <= 0) {
             //Dead();        
         }
         
     }
 
     public void TotalScoreLeft (int playerScoreLeft)
     {
         totalScoreLeft = playerScoreLeft;
         Debug.Log ("Score is being added left" +totalScoreLeft);
 
     }
 
     public void TotalScoreRight (int playerScoreRight)
     {
         totalScoreRight = playerScoreRight;
             Debug.Log ("Score is being added right" +totalScoreRight);
         
     }
 
     public void TotalScore ()
     {
         totalScore = totalScoreRight + totalScoreLeft;
         print ("Total score is: " + totalScore);
         Debug.Log ("Total score is: " + totalScore);
     }
 
 
     public void Dead(){
         Destroy (gameObject);
     }
 
     void OnGUI() {
         GUI.Label (new Rect (1500,25,50,50), health.ToString());
         
     }
 }


avatar image Takzdeveloper · Aug 07, 2014 at 11:40 PM 0
Share

ScoringAndEnemyDestroyLeft referring to the left collider and the PlayerHealth script is where i'm trying to total the values at the moment, I will move the total function to a different script when I figure it out, or if you guys help me figure it out :D.

Thanks

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to pass and retrieve values to 2 dimensional array 1 Answer

Getting parameter values sent to a method c sharp script 0 Answers

rigidbody2D.velocity returns zero values 1 Answer

Can't assign dictionary values 0 Answers

Adding elements to a list is changing the initial values. Need help! 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