Wack-a-Mole Share Thread

Share all your variations of the Wack-a-Mole game here. Tell us what you did so others can learn from it. Videos and images would be awesome.

Well I gave it my best shot. I really enjoyed the programming lectures, I feel they’re really helping me get a better understanding of the fundamentals. Also getting a lot more confident using the Unity software.

I wasn’t sure about the are spawn radius and if there was a way to create a parameter where they only spawn along a X axis value of 0 (In trying to get them to appear onto the surface of a plane)

Also I tried to get some audio involved, I tried to get it to play an Audio Clip on collision however it proved to difficult and I just returned to working on the models, and just added audio sources that played on awake.

The UI was a bit tough for me too, but will continue to work on it.

3 Likes

I spent a fair amount of time investigating into running a Score counter, keeping tally over the number of Moles (or in your case Digletts) hit.

At first I jumped to the idea of adding a counter onto the “moles” themselves, but that wouldn’t work as each individual mole that spawns would create its own instance of the script, so all scripts would be 0 until a projectile collided with them; at which point the script turned to one and the object (along with the script) was immediately deleted. It became clear that I’d need to keep the score on another game object. Since the score’s text will be kept on a UI.Text GameObject, I found it made sense to also tie the “ScoreScript.cs” script to the Text GameObject.

Problem then lay in updating this script on the Text GameObject whenever the mole game object deleted itself. How can you get each ‘mole’ to tell another game object when it’s about to break? If you keep tally on each mole and then ask the ScoreScript.cs to look at every mole, it never gets a chance to look since the instance the mole gains a point it deletes itself, with no potential gap for the ScoreScript.cs to find the destruction count.

Instead I found I needed to modify the MoleLifetime.cs. This is the script that is tied directly to each Mole (through the mole prefab). To get hold of the ScoreScript.cs on the Text object didn’t make itself easy, you needed a new command, FindObjectOfType . This command searches through the Hierarchy for any GameObject linked to the script you request, and then connects the script to a local variable (the same way GetComponent<> works).

By connecting the MoleLifetime to the ScoreScript inside the OnCollision method, you could get each MoleLifetime to call a method on the ScoreScript.cs script every time one of the moles was hit by a projectile. All it took was writing a short method in ScoreScript.cs to keep a counter, and you had a float attached to the Text file, now successfully keeping count of the targets hit.

Below is a copy of my “OnCollisionEnter” text block post-modification, blurred in-case anyone actually read through this essay of a post wants to try the logic of this themselves before copying the result straight from this document (left-click on blurred text to reveal).

void OnCollisionEnter(Collision collision) {
. ScoreScript localScore = FindObjectOfType();
. localScore.ScoreUpdater();
. print(“Score!”);
. Destroy(collision.collider.gameObject);
. Destroy(gameObject);
}[/spoiler]

note that ScoreScript is the name of my script on the Text, and ScoreUpdater is a method on ScoreScript adding one to the float count every time it is called.

The final part of the task is getting the ScoreScript to update the text box of the UI’s Text component so that the tally actually prints into the text, rather than into the command line. This requires 2 things. First is to link UnityEngine.UI to the top of your C# script, in the same way you have the text “using UnityEngine;” at the start of every C# script you create. This line adds a load of new tools that you can use in your code to modify different parts of the canvas UI. The tool we’re most interested in is Text (here’s a decent explanation of the tool).

All that is needed here is to create a Text variable, make it GetComponent from the Object it is attached to, then modify that variable to be the score you want to print. Here is the result of my ScoreScript.cs in it’s entirety (blurred again):

[spoiler]using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreScript : MonoBehaviour {
. private float answer = -1;
. void Start () {
. . ScoreUpdater();
. }

. public void ScoreUpdater() {
. . answer = answer + 1;
. . Text scoreText = GetComponent();
. . scoreText.text = ("Score: " + answer);
. }
}

As an explanation for why I started the script at -1 and not 0, I placed the code into the ScoreUpdater to update the UI (why update the number count every frame when you can update it every time the number raises 1?). This lead to the textbox starting showing whatever rubbish was inside the textbox before the script updated it. Instead I simply ran the ScoreUpdater script once on startup.
I could have just set the textbox to start filled with “Score: 0”, but that would have lead to longer development times as each run I would have to shoot a mole with a projectile to see if the score was updating. Just an FYI in case anyone wonders about this.

I realise I’ve written a small book worth of notes here, but I hope that maybe someone will find use of this research. This is my 3rd computer language that I have learnt the fundamentals of, and each and every one turns out to have these small commands hidden with a relatively simple solution. In this case my solution wasn’t perfect, I was forced into making the Moles each search through the whole hierarchy every time they wanted to contact a script that was never moving. I’m sure that somewhere there must be a direct way to link the two scripts without searching through the Hierarchy, but this is certainly a functional method for the task at hand. Unless your launching thousands of projectiles into one scene without reloading, I doubt you’ll run into any processing errors.

That said if anyone finds a simpler solution to linking the 2 scripts, please post away. I’d be happy to improve the code. Good luck to everyone on there future projects! :smile:

I just realized I didn’t download the Mole Pack code, so I need to go through that and my own code to see the correct way to implement the stuff I did.
Anyhoo, here are a couple pics.
The coolest new things I had to look up were:

  • using SetSiblingIndex to hide part of the UI.
  • setting angularVelocity before calling AddForce to make the crates spin.
  • learning the difference between the Animation and Animator components, and how a Controller is needed to enable playing animations.
  • using a switch/case to increase difficulty (not sure it was the best solution).
  • using an enum to keep track of game state.
  • using GameObject.Find to get a reference to another object so I could mess with it. This was especially handy because I had an empty game object with a script attached that held my game state and score data.
  • using BroadcastMessage to call the same method in multiple scripts. (Update: Ben recommends using C# delegates instead of trying to broadcast a message to all objects, which isn’t really possible without serious manipulation in code.)

Play it at https://pookiemitchell.itch.io/bop-a-mole.
The video is at https://youtu.be/vnM-96GaHhA.
The code is at https://github.com/janetfi/BopAMole.

Thanks for the tip about itch.io, Sam!!

1 Like

Great work. This game is absolutely awesome. Have you considered putting it up on itch.io?

1 Like

:smiley: Thanks
Is there some trick to uploading a game to itch.io? The Unity option says “Unity <= 5.3”, so … I guess those of us using Unity 5.3.4f1 have to select “HTML” or “Downloadable”?

I would go with HTML anyway because it gives a better output.

Hey guys! Took me some time but I made a pop the bubbles game. I got the idea from watching Kung fun panda xD It was so HELPFUL taking the time to do this project. It really helps cement the fundamentals and principles. I feel like it all makes more sense as I keep messing around and creating games.

Here is the link:
https://gamebucket.io/game/8ed74cc7-4087-41e2-9644-dd8c0f7c3d8e

Privacy & Terms