Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Yeah I know what's the problem.
- You weren't clear when you wrote request. I'm also confused :D
- Okay so check this out :
- if (moneyAmount == 0)
- {
- // In plain english we say :
- // If moneyAmount is equal to 0
- moneyAmount = score;
- // We will set value of moneyAmount to value
- // of score
- }
- else if (moneyAmount != 0)
- {
- // but we will not do that if moneyAmount value is not 0
- // instead, we will add value of score to moneyAmount value
- moneyAmount += score;
- }
- // Also, you are writing this code in void Start() which only gets called once when script gets initialized.
- Do you see the problem ? Code is working fine but your logic is wrong.
- So to make this happen we can do something like this :
- int moneyAmount = 0; // We will use this to track current score
- int bestScore = 200; // Max score ever
- void AddToScore(int value)
- {
- // All we do here is we add
- // value to out score
- moneyAmount += value;
- }
- void AddOneCoinToScore()
- {
- moneyAmount++;
- }
- void GameOver()
- {
- // You can call this function when player dies
- if (moneyAmount > bestScore)
- // If moneyAmount player had when he died
- // is greater than best score
- {
- // Set best score to be moneyAmount
- bestScore = moneyAmount;
- }
- }
- Now you probably have some way to check when player gets one point right ?
- When player gets one coin, all you have to do is write :
- AddOneCoinToScore();
- When player dies, all you have to do is call :
- GameOver();
- And if you want to add custom value of points to score, you can call :
- AddToScore(5);
- Here's an example again :
- using UnityEngine;
- using System.Collections;
- public class add : MonoBehaviour {
- int moneyAmount = 0; // We will use this to track current score
- int bestScore = 10; // Max score ever
- float timer = 1.0f; // Debug test variable
- float DieAfter = 5.0f; // Debug test variable
- void AddToScore(int value)
- {
- // All we do here is we add
- // value to out score
- moneyAmount += value;
- Debug.Log("Added " + value.ToString() +
- "coin(s) to moneyAmount. Money : " +
- moneyAmount.ToString());
- }
- void AddOneCoinToScore()
- {
- moneyAmount++;
- Debug.Log("Added one coin to moneyAmount. Money : " + moneyAmount.ToString());
- }
- void GameOver()
- {
- // You can call this function when player dies
- if (moneyAmount > bestScore)
- // If moneyAmount player had when he died
- // is greater than best score
- {
- // Set best score to be moneyAmount
- bestScore = moneyAmount;
- Debug.Log("Player Died, new high score :" + bestScore.ToString());
- }
- }
- void Update()
- {
- // This is just debug example and is not relevant
- // to your code, it just shows you how to call
- // functions
- timer -= 1f * Time.deltaTime;
- DieAfter -= 1f * Time.deltaTime; ;
- if (timer <= 0)
- {
- AddOneCoinToScore();
- timer = 1.0f;
- }
- if (DieAfter <= 0)
- {
- GameOver();
- DieAfter = 5.0f;
- }
- }
- }
- ![alt text][1]
- [1]: /storage/temp/37560-example.jpg
- I hope that it's more clear now ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement