# Untitled

a guest
Dec 23rd, 2014
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. Yeah I know what's the problem.
2. You weren't clear when you wrote request. I'm also confused :D
3.
4. Okay so check this out :
5.
6. if (moneyAmount == 0)
7. {
8. // In plain english we say :
9. // If moneyAmount is equal to 0
10.
11. moneyAmount = score;
12. // We will set value of moneyAmount to value
13. // of score
14. }
15.
16. else if (moneyAmount != 0)
17. {
18. // but we will not do that if moneyAmount value is not 0
19. // instead, we will add value of score to moneyAmount value
20. moneyAmount += score;
21. }
22.
23. // Also, you are writing this code in void Start() which only gets called once when script gets initialized.
24.
25.
26. Do you see the problem ? Code is working fine but your logic is wrong.
27.
28. So to make this happen we can do something like this :
29.
30.
31. int moneyAmount = 0; // We will use this to track current score
32. int bestScore = 200; // Max score ever
33.
34.
35. void AddToScore(int value)
36. {
37. // All we do here is we add
38. // value to out score
39. moneyAmount += value;
40. }
41.
43. {
44. moneyAmount++;
45. }
46.
47. void GameOver()
48. {
49. // You can call this function when player dies
50.
51. if (moneyAmount > bestScore)
52. // If moneyAmount player had when he died
53. // is greater than best score
54. {
55. // Set best score to be moneyAmount
56. bestScore = moneyAmount;
57. }
58. }
59.
60.
61. Now you probably have some way to check when player gets one point right ?
62. When player gets one coin, all you have to do is write :
64. When player dies, all you have to do is call :
65. GameOver();
66.
67. And if you want to add custom value of points to score, you can call :
69.
70. Here's an example again :
71.
72. using UnityEngine;
73. using System.Collections;
74.
75. public class add : MonoBehaviour {
76.
77. int moneyAmount = 0; // We will use this to track current score
78. int bestScore = 10; // Max score ever
79. float timer = 1.0f; // Debug test variable
80. float DieAfter = 5.0f; // Debug test variable
81.
82. void AddToScore(int value)
83. {
84. // All we do here is we add
85. // value to out score
86. moneyAmount += value;
87. Debug.Log("Added " + value.ToString() +
88. "coin(s) to moneyAmount. Money : " +
89. moneyAmount.ToString());
90. }
91.
93. {
94. moneyAmount++;
95. Debug.Log("Added one coin to moneyAmount. Money : " + moneyAmount.ToString());
96. }
97.
98. void GameOver()
99. {
100. // You can call this function when player dies
101.
102. if (moneyAmount > bestScore)
103. // If moneyAmount player had when he died
104. // is greater than best score
105. {
106. // Set best score to be moneyAmount
107. bestScore = moneyAmount;
108. Debug.Log("Player Died, new high score :" + bestScore.ToString());
109. }
110. }
111.
112. void Update()
113. {
114. // This is just debug example and is not relevant
115. // to your code, it just shows you how to call
116. // functions
117. timer -= 1f * Time.deltaTime;
118. DieAfter -= 1f * Time.deltaTime; ;
119.
120. if (timer <= 0)
121. {
123. timer = 1.0f;
124. }
125.
126. if (DieAfter <= 0)
127. {
128. GameOver();
129. DieAfter = 5.0f;
130. }
131. }
132.
133. }
134.
135.
136. ![alt text][1]
137.
138.
139. [1]: /storage/temp/37560-example.jpg
140.
141. I hope that it's more clear now ?