To provide feedback to the Player on how much money they receive from collecting the coins I decided to create a UI pop-up that displayed the numerical value the coins provide the Player (as seen in Figure 4.)
Figure 1. |
Figure 2. |
Figure 2. is the PopUpTextManager. This script controls where the UI will be instantiated; as a UI element the Text needs to be instantiated as a child of the Canvas, otherwise they won't appear on screen.
Figure 3. |
Figure 3. is the older script I made, MoneyPickUp, which controls what happens to the coins when they are Moused over, such as adding money to the Player's total funds. So this is where we call the PopUpTextManager to run the functions within that script.
Figure 4. |
Updated Sound Feedback Production:
I used to generate the sound for the coins by instantiating a prefab which contained the Audio Source component when the coins where moused over. As can be seen in Figure . however this was causing issue in the editor. Unity refused to destroy the clones of the sound prefab and they cluttered the inspector. Now due to the length of our level's gameplay, I knew that this wouldn't cause an issue on performance, and I was reinforced in my belief after a few test builds.
However in my latest test build (17/04/2017) I could no longer "pick up" the coins. They spawned, the text feedback appeared but they would not give the player money, nor generate a sound or destroy themselves once moused over as can be seen in Figure .
I had a hypothesis as to how to fix this, and that was to clean up my original code for generating the coin pick-up sound. So instead the inspector contains an empty GameObject which itself contains the Audio Source component which contains the coin pick-up sound. in the code I removed the instantiate and the Destroy (prefab, delayed by seconds) lines and replaced them with a void Start method which went and found the empty GameObject containing the Audio Source component by name, and then used GetComponent to get the Audio Source component. Then I played it when appropriate, which is in the OnMouseOver function.
This cleared up the stockpile of red errors Unity spat at me informing me that the prefab could not be destroyed and with another test build the coins work as they should both in Unity and in the game build.
This cleared up the stockpile of red errors Unity spat at me informing me that the prefab could not be destroyed and with another test build the coins work as they should both in Unity and in the game build.
No comments:
Post a Comment