- Home /
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.
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) :)
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
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.
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!
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());
}
}
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
Follow this Question
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