Tag Archives: pong

Beginner’s Guide: Create a Pong Clone in Unity: Part 10

Polishing it Off

We’re now at the end of our fun, enlightening, and probably exhausting journey to recreate Pong. In this final part we will add polish to the game and learn a few more titbits of Unity wisdom before signing off on Pong (or whatever you’ve called your version). Then we’ll build a standalone version of the game that can run on any computer without needing Unity. If you have access to a mobile device, I’ll touch a little bit on building and deploying to those, but with so many platforms and other variables, it’s outside of our scope to go into detail about that.

Snazzy Effects

We’ll add a few cool effects to our game to really up the fun factor. The best games have a lot of ‘juice’ – extra touches that make every frame addictive fun. Think of the Candy Crush clunk sounds or Angry Birds’ crashing, tumbling blocks. Granted, our Pong clone is not the height of fun or sophistication, but with these extra touches we’ll prove that even the simplest of games can be improved a lot by a few flourishes.

Shake the Camera

A simple, effective drop of juice is the camera shake. You see it in lots of games, and it gives a sense of kinetic energy, augmenting sound and vision to create something physical on a flat screen.

If you recall all the way back to Part 1 you may remember that our scene (and all scenes) has a default camera. You’ll also recall that all GameObjects (the camera is a GameObject) have a transform component, which determines its placement, rotation, and size. Therefore, if you move the camera’s transform you are effectively moving the view of the scene, or – from the perspective of the player – you’re moving the game area. If you quickly move the camera in small, random jolts, the screen will shake!

We will add a script to our MainCamera GameObject with a simple Shake() method, and we’ll call this method from GameManagerScript.

Create a new C# script called CameraShakeScript (remember to place it in the Scripts folder). Paste the following code into the script:

using UnityEngine;
using System.Collections;

public class CameraShakeScript : MonoBehaviour {
    // remember the correct starting camera position so we can revert back once shaking is complete
    private Vector3 originPosition;
    private Quaternion originRotation;

   public float originalDecay = 0.006f;
   public float originalIntensity = 0.04f;
   float shake_decay;
   float shake_intensity;
   private bool shaking; // is the camera supposed to be shaking at the moment
   Transform cameraTransform;

   void Start() {        
       cameraTransform =  Camera.main.transform;        

   void Update (){

       if (shake_intensity > 0f)
            cameraTransform.localPosition = originPosition + Random.insideUnitSphere * shake_intensity;
            cameraTransform.localRotation = new Quaternion(                
            originRotation.x + Random.Range (-shake_intensity,shake_intensity) * .2f,                
            originRotation.y + Random.Range (-shake_intensity,shake_intensity) * .2f,                
            originRotation.z + Random.Range (-shake_intensity,shake_intensity) * .2f,                
            originRotation.w + Random.Range (-shake_intensity,shake_intensity) * .2f);
            shake_intensity -= shake_decay;

            shaking = false;
            // reset the camera to its original state
            cameraTransform.localPosition = originPosition;            
            cameraTransform.localRotation = originRotation;            

   public void Shake(){

      if(!shaking) {            
        originPosition = cameraTransform.localPosition;            
        originRotation = cameraTransform.localRotation;

      shaking = true;        
      shake_intensity = originalIntensity;        
      shake_decay = originalDecay;

I won’t go into detail about this code, but here’s what it does:

When Shake() is called, the camera is moved around randomly based on the intensity and decay variables (the decay determines how long the shake lasts and the intensity determines how much movement there is). Every frame the camera will shake if the shaking boolean variable is set to true. Once the shake is finished that value reverts to false.

  • Save that script and attach it to the GameManager GameObject.

Open GameManagerScript. We’ll use Unity’s find method to get a reference to the camera to demonstrate the syntax a bit more, but you could just as easily use [SerializeField] and drag CameraShakeScript into the empty field in the Inspector.

Add this variable to the script:

CameraShakeScript camShake;

Now, in Start(), populate the variable so it can be used in the script (place this line before the StartNewGame() call:

camShake = GetComponent<CameraShakeScript>();

Now we can make the camera shake by calling the Shake() method on cameraShake like this, which you need to add to the GoalScored() method at the start, so the camera will shake whenever a goal is scored:


Test the Shakes

Run the game and see the shake effects. Nice. The shaking really ‘sells’ the action…

You can experiment with the shake by adjusting the Original Decay and Original Intensity variables via the Inspector. Can you modify the script to shake the camera every time the ball hits a wall?


A common technique used to create effects is the particle system. A particle system is a graphical effect created by an algorithm. Smoke is usually a particle effect. Particle effects let you create attractive effects with relatively little work (and, incidentally, low processing power).

Our particle effect is going to be rudimentary, but it will give you an idea of how particle systems work. We will display an ‘explosion’ of debris when a goal is scored.

Like everything else, a particle system is a component. We will attach our particle system to the goals, and to do this we will add it to the goal prefab.

Create a new ParticleSystem in the Hierarchy:

  • GameObject | Particle System
If you don’t see the particle system, double-click the new GameObject in the Hierarchy to centre the scene view on it. You may need to zoom in.

It should look like this:

We’ll change this to make a few bits of white debris to fly off the goal when a goal is scored.

With the particle system selected in the Hierarchy you’ll notice it has a lot of settings in the Inspector:

Make the following modifications:

  • Change Duration to 0.25 (how long the particle system shoots out particles)
  • Change Start Lifetime to 0.5 (how long a particle sticks around for)
  • Untick Play On Awake (we don’t want the particle system to automatically start)
  • Change Emission Rate to 40 (click the Emission heading to expand it first)
  • Make the particle system a child of Player1Goal.
  • Reset the particle system’s transform (i.e. set its position to 0, 0, 0 or click the cog in the Transform component and choose Reset).
  • Make the Particle System GameObject a child of Player1Goal (drag-and-drop it in the Hierarchy).
  • Reset the particle system’s transform by clicking the cog and selecting Reset in the Inspector with the Particle System GameObject selected. This sets the particle system’s transform to match the goal it’s attached to.
  • Change the Particle System‘s transform’s rotation values to X = 0, Y=90, and Z = 0. This makes the particles shoot out from the goal.


  • Lastly, untick the Looping property of the particle system. This will turn off the particle simulation.

Now we’re going to do something new. We’re going to ‘re-prefab’ our GoalPrefab. This means we are going to update GoalPrefab with changes we’ve made to one of its instances. To do this, drag-and-drop Player1Goal from the Hierarchy onto GoalPrefab in the Prefabs folder (you’ll need the Prefabs folder to be open in the Project window). Once you’ve done this the particle system will become part of the prefab, and Player2Goal will automatically get a matching particle system! That’s the power of prefabs, sir or madam!

Because Player2Goal is a carbon copy of Player1Goal the particle on Player2Goal will face in the same direction – but it needs to be facing the opposite direction, away from its own wall. To fix this, simply change Player2Goal’s Particle System Transform Rotation X value to 180 in the Inspector. This flips it on its X axis.

Script the Particle System

Before we can see our particles in action we need to fire it off at the appropriate time. Open GoalScript.

Insert this new variable:

ParticleSystem partSys;

Then change the OnCollisionEnter2D() method to this:

void OnCollisionEnter2D(Collision2D other)
    if(other.transform.name == "Ball")
        partSys.transform.position = new Vector2(partSys.transform.position.x, other.transform.position.y);

Here we move the particle system to the same position as the ball at the moment it hits the goal, then play the particle emission at that location so it looks like some debris is being chipped off the goal by the ball. Snazzy.

  • Save the script then head back into Unity.

For each of the player goal GameObjects:

  • Select the object, then drag its particle system child into the Part Sys field in the Goal Script Inspector entry. This populates the partSys variable with the particle system so the script has something to play.

Play the game…

Game Over

We’re done! We’ve completed our Pong clone. It’s no masterpiece, but hopefully it has taught you the main building blocks of creating a game in Unity. If you look back through these ten lessons you’ll see that you’ve learned a lot, and hopefully some of it has stuck! You should have a pretty solid grasp of GameObjects, MonoBehaviour scripts, components, the Inspector, and so on, perhaps enough to embark on your own game.

There are also lots of ways to improve on our Pong clone. Here are some suggestions:

  • Improve the UI.
  • Animate something in the main menu screen (a bouncing ball perhaps).
  • Stop the ball after every goal and have a 3…2…1…GO timer before play resumes.
  • Add some extra effects:
    • A bounce sound when the ball hits a wall
    • Flashing when a goal is scored
  • Implement an AI opponent for single-player action
  • Show the score on the End Game screen.

I hope you’ve learned a lot from these tutorials and – more importantly – I hope you’ve had fun!

Beginner’s Guide: Create a Pong Clone in Unity: Part 9

Wired for Sound

In this part of our Pong tutorial series we will finally add sound to our game! We’ll also get a bit more practice with scripting and the Unity UI system.

Sound Assets

You probably know the drill by now – create a new folder within Assets called ‘Audio’, and drag the sound files from the assets .zip into that folder (you can download the assets .zip file here). They will automatically have the correct names based on the file names.

We want to add sounds when the game is played, so start by opening the main game scene (Scene1).

  • Select GameManager in the Inspector
  • Add an AudioSource component (in the InspectorAdd Component | Audio | AudioSource).

An AudioSource is a component that plays audio in your scene.

 An AudioClip is an actual sound component.

Update GameManagerScript

We need to add variables that hold references to our sounds in GameManagerScript so we can play them at our leisure. Open GameManagerScript and add the following variables:

AudioClip goalScored;
AudioClip endGame;

Also add this variable, which we’ll treat a bit differently:

AudioSource audSource;

Save the script.

Remember, until you save a script, any serialized (or public) variables you have added won’t appear in the Inspector.

Drag the relevant audio clips onto the above variable fields via the Inspector in the same way we’ve done for other components in previous parts. This lets us populate those variables with the correct sound file.

We could (or rather should) do the same with the AudioSource (i.e. drag a component into the Inspector), but we’ll use a different method just so you can see this other method in use. Unity has ways to ‘find’ a GameObject or component within the current scene (or the GameObject that the current script is attached to). We’ll use one of those ways to find the AudioSource.

Add the following to GameManagerScript‘s Start() method (it doesn’t matter whereabouts in the method this goes):

audSource = GetComponent<AudioSource>();

That line looks through the GameObject this script is attached to and finds a component of type AudioSource, then puts a reference to it in our audSource variable. A similar method lets you find a GameObject:

// not code for your Pong game, just sample code to illustrate
player = GameObject.Find("Player1");

There are a few variations on these that we won’t go into, but you can find anything in your scene using these finder methods.

Searching for Unity components and GameObjects can be very slow (or ‘expensive’ in programming terms). You should never search for a component or GameObject during any of your game’s update methods (Update, FixedUpdate, or LateUpdate) or inside loops, as code can run very often, compounding the slowness. If you need to use a specific GameObject or component in repeating code (such as in Update()), get the reference once and store it in a variable, then use the variable in your loop or Update() method. 

We need a method to play a sound, so add this method to GameManagerScript:

void PlaySound(AudioClip soundClip)
    audSource.clip = soundClip;

That should be pretty easy to understand. We must call this method any time we want a sound to play. We send the specific sound clip as a parameter so the method knows which to play. The audSource variable is our audio source, and the soundClip parameter is the specific sound we want to play via the audio source.

Add Calls to PlaySound()

Now, let’s add our calls to PlaySound(). At the beginning of the GoalScored() method, add:

PlaySound (goalScored);

At the end of the GameOver() method, add:

PlaySound (endGame);

End Game Fix

The next thing we need to do is clear up a gameplay issue that’s currently in the game. When a player reaches 3 points, the game resets abruptly. We will add a popup to congratulate the winner and provide a ‘New Game’ button to restart the match.

Open BallScript and create this new method that we can access from other scripts to stop the ball moving:

public void Stop()
    // this method stops the ball
    myBody.velocity = Vector2.zero;

While you have BallScript open we will make a slight change so that the ball doesn’t always start off traveling in the same direction after a goal is scored.

At the end of the Start() method, add this extra line:

forceValue = forceValue * -1;

That line effectively reverses the ball’s starting direction every time the game resets after a goal is scored. This works because the screen centre is (0,0), meaning a positive force value will push the ball to the right, and a negative value will push the ball left. Multiplying forceValue makes sure that the next time the ball starts it will go the other way. A simple little hack, but it works.

Save BallScript to apply the changes.

Now open GameManagerScript and find the GameOver() method. Replace the line:




Now add the following new method to GameManagerScript:

public void StartNewGame()
     playerOneScore = 0;
     playerTwoScore = 0;
     UpdateScoreText ();
     gameBall.Reset ();

This method just needs to tell the ball to start again, and we will trigger it to run when our ‘New Game’ button is pressed.

Before we get to creating our end game screen, delete the following lines from the GameOver() method (as we have now placed them in the StartNewGame() method instead):

playerOneScore = 0;
playerTwoScore = 0;
UpdateScoreText ();

This just means the score stays visible until the game is reset so the winner can gloat. Save the script and go back into Unity.

EndGame Popup

Create a new GameObject and call it EndGame. Make EndGame a child of Canvas by dragging it into the Canvas GameObject.

Add three new empty GameObjects as children to EndGame with the following names (and in the following order in the Hierarchy):

  • Background
  • ResetButton
  • GameOverText.

Unity will render items in the order they appear in the Hierarchy, and whatever is rendered last renders on top of everything else. We need our text and button to be rendered on top of the background, so we set Background higher in the Hierarchy.

With the new GameObjects created the Canvas should look like this in the Hierarchy:

  • Give ResetButton a UI Button component.
  • Give GameOverText a UI Text component.
  • Select GameOverText in the Hierarchy and in the Inspector add “Game Over” in its Text field.
  • Style to text to be quite large, and move the transform so the text appears in the middle of the screen. (You may need to change the Width property of the Rect Transform in the Inspector to fit all the text on the screen.
  • Drag the Bangers font into the Font field as well, and change the text colour if you want.
  • Select ResetButton in the Hierachy.
  • Add a UI Image component and drag in the PlaySprite that we used on our main menu screen.
  • Within the Inspector, drag the Image (Script) component onto the Target Graphic field of the Button(Script) component just like we did on the main menu button earlier.
  • Reshape the Rect Trnasform to make the button look less stretched (again, just like we did earlier). If you select the Scale tool in the toolbar in the top-left of the Unity screen you can resize transforms easily by dragging the axis lines with the squares on the end (just like how you can move the transform in Transform mode by dragging the arrows):


  • Add a UI Image component to Background.
  • Change Background’s Color property in the Inspector (on the Image (Script) component). Choose a colour to act as a background. This will form a box to display our little game over screen (if you want to add an image instead, go ahead – it works just like adding any other image).
  • Stretch and position Background’s Rect Transform to look something like this:

If you can’t see the text or button, make sure Background is listed above them in the Hierarchy.

Next we need to make the button restart the game. To achieve this: 

  • Select ResetButton in Hierarchy.
  • In the Inspector, click the + at the bottom of the Button Script() component’s On Click() events area.
  • Drag the GameManager GameObject from the Hierarchy into the empty field currently containing None (Object).
  • In the drop-down menu (that currently reads ‘No Function’, select GameManagerScript | StartNewGame().

Now you might (rightly) be thinking ‘wait, we’ve covered our game screen with this menu’. We must implement a way to show and hide the menu. 

Show and Hide the Menu

Unity lets us turn GameObjects on and off via the SetActive() method. We’ll add a reference to the EndGame GameObject in GameManagerScript, then at the relevant times we will set EndGame’s active status to on (true) or off (false).

Add a serialized field to GameManagerScript:

GameObject endGameScreen;

Now you must populate that serialized field with the EndGame object. Select GameManager in the Hierarchy and drag EndGame onto the End Game Screen field.

Let’s clean up some code as well. You will notice that the Start() and StartNewGame() methods have some identical code. We don’t need repetition. We don’t need repetition. We don’t need repetition. Replace those two methods with the code below, to which I’ve also added a command to hide the end game screen:

public void StartNewGame()
    playerOneScore = 0;
    playerTwoScore = 0;
    UpdateScoreText ();
    endGameScreen.SetActive (false);
    gameBall.Reset ();

void Start ()
     audSource = GetComponent<AudioSource>();
     StartNewGame ();

Add this to the GameOver() method, just after the gameBall.Stop() line:

endGameScreen.SetActive (true);

That turns ON the end game screen when the match ends.

Now disable EndGame by default by turning off its transform. Select EndGame in the Hierarchy, then untick the little checkbox in the upper-left corner beside its name:

This also hides it while we’re in Unity working on our game.

Test the Game

Now hit the Play button and try out the game…

…weird, huh? The ball just moves up and down! It’s almost like we’ve taken a step backwards…

What happened is that when we changed GameManagerScript we inadvertently made the Ball’s Start() script execute twice, pushing the ball in one direction, then immediately in the opposite direction. This sort of thing can happen when you access public methods and variables between scripts, which is one reason you should avoid public whenever possible.

Fix BallScript

Let’s rework BallScript a bit to remedy the above problem. Open BallScript. As you see, the Reset() function calls the Start() function. We also call Reset() from GameManagerScript whenever a new point or game starts.

Cut the following lines from Start() and paste them into Reset() at the end of the method:

myBody.AddForce (new Vector2 (forceValue * 50, 50));
forceValue = forceValue * -1;

Also, remove the call to Start() from within Reset(). Save the script, save the scene, then playtest the game. That’s better!


A lot of what we did was familiar, such as populating fields that were exposed in the Inspector via [SerializeField]. We added audio clips to GameManagerScript this way. We then used references to those audio clips to play the sounds we wanted at specific times during the game. OK, the sounds aren’t exactly thrilling, but hey, you’ve learned how to add sounds to a Unity game, right?

We also touched upon Unity’s ways of finding components and GameObjects from a script via the GameObject.Find and GetComponent methods. We learned that these shouldn’t be used in any code that frequently repeats.

We did a bit more work with Unity 4.6’s new GUI system to create our end game screen, and learned a few more scripting techniques in the process. We went and added a few improvements to our scripts. The game still needs a little more…juice.

More about that in Part 10.

Beginner’s Guide: Create a Pong Clone in Unity: Part 8

What’s on the Menu?

By the end of Part 7 we had a mostly feature-complete Pong clone. But there’s still work to do, and there are still lots of cool things to learn about using Unity.

In this part we’ll dip our toes into Unity graphical user interface (GUI) features to display the score, create a menu screen, and do some other UI stuff.

Unity’s GUI

The Unity GUI system works via a canvas. You add a Canvas object to your scene, and then add children to this canvas with text, buttons, and so on. The Canvas (along with its UI) is overlaid on your game screen to present menus, information, health bars, and so on.

Add a Canvas

  • Add a Canvas to the scene via the main menu GameObject | UI | Canvas.

Select the Canvas in Hierarchy and look at its contents in Inspector. You’ll see a bunch of stuff that Unity needs to draw the GUI. The most obvious difference is that instead of the usual Transform, GUI objects have a Rect Transform:


Add Text

With Canvas selected in Hierarchy, add a UI | Text component in the Inspector:

  •  In the new component, in the Text field, type “Player One 0 – 0 Player Two”. This is ‘placeholder’ text.
  • Change the Color property (just like we did for the paddles earlier) so the text is clearly visible (view the text on the Game screen at the top-left corner).
  • Centre the text and align it to the top of the screen.

You can copy the settings from the following image:

You will see the text in the Scene and Game windows. Pay attention to how it looks in the Game window rather than the Scene window, as the Canvas GUI view can be confusing at first.
o ensure the text stays the same size regardless of screen resolution, set the Ui Scale Mode field in the Canvas Scaler (Script) component to Scale With Screen Size:

Awesome. But of course we need that score to change throughout the game. Let’s do that now.

Draw the Score

We need modify GameManagerScript to change the score text whenever the score changes.

Open GameManagerScript. Unity’s GUI stuff has its own namespace, which isn’t in MonoBehaviour scripts by default.

namespace is a way for organizing code. Your scripts can only access code within their own namespace or in namespaces they explicitly include. This helps keep code modular.

To add the namespace, add the following code at the top of the script (you can see where it belongs as there are some other lines very similar at the top):

using UnityEngine.UI;

GameManagerScript also needs a reference to the score text component, so add this to the variables list in GameManagerScript:

Text scoreText;

Save the script.

With GameManager selected in Hierarchy, drag-and-drop the Canvas GameObject onto the Score Text field in the Inspector.

Add UpdateScoreText() Method 

Open GameManagerScript and add the following method:

void UpdateScoreText()
    scoreText.text = "Player One " + playerOneScore.ToString() + " - " + playerTwoScore.ToString() + " Player Two";

This changes the GUI Text component to show the scores by combining a few words with the variables that store the player scores.

Call the UpdateScoreText() Method

Now we need to make sure that method is called whenever a goal is scored, and also when the game restarts (i.e. when the score is reset). Find the GoalScored method and add the following line to the very end of the method (before the curly brace, of course):


And in the GameOver method, again at the end, add the following line:


Great. Now the score text changes whenever the scores change in the game!

Try it out. How cool is that?

Would you Like to See the Menu?

Since we haven’t yet done anything with scenes, and all games need a menu, let’s create a menu scene/screen.

Create a new Scene

Save the current scene: menu File | Save Scene.

Now create a brand new scene: menu File | New Scene. Save this new scene (File | Save Scene) in the Scenes subfolder, and name it ‘Menu’.

Add this new scene to the build settings (File | Build Settings, then Add Current). Then, using drag-and-drop, move the scenes so that Menu is before Scene1 in the list:

We’ve now added our scenes to the build settings so they will be included whenever we build a copy of this game. When this game launches from a built copy it will start with the scene at the top of that list, which is the Menu scene.

When you hit the Play button to test your game, it launches the scene you have open. Be aware that sometimes your scenes can rely on actions and data from previous scenes in order to work correctly.

Close the Build Settings dialog and get back to building our menu.

Build a Menu

Our main menu screen needs a few basic elements:

  • A game title
  • A programmer credit
  • A background
  • A Start button.

We will add those items to a UI Canvas, just like the way we added score text earlier.

Add a Canvas to the scene: menu GameObject | UI | Canvas.

Add a UI Text component to that canvas and modify its properties to display the game title (you can call it simply ‘Pong’ or come up with your own name) in a pretty large font near the centre of the screen.

See if you can figure out how to add the font included in the assets zip file to make your text look as funky as mine.

Aim for something like this (but feel free to play around – this is your game after all):

Set the canvas to scale with the screen size like we did earlier with the score text.

Add a Byline

Because we can only add one Text component to a GameObject, we need to create our byline as a separate GameObject, but we need it to be a child of the UI canvas.

  • Create an empty GameObject and rename it to Byline.
  • Drag the Byline GameObject onto the Canvas GameObject so it becomes a child of Canvas.
  • Customise Byline’s
    Text component to show your byline (e.g. ‘By Fred Bloggs’). Use the same techniques as for the game title to change the text size and colour.
  • With Byline selected in Hierarchy, drag the byline transform in the Scene view to position it nicely below the game title.

Now your screen should look something like this:


You can add a background image or change the background colour if you like. To add a background image you can add a new GameObject as a child of Canvas and give it a UI Image component. Then drag in any image you want to use as your background.

To change the background colour you can simply change the camera’s background:

  • Select Main Camera in Hierarchy
  • Change the Background property in the Camera component in the Inspector.

Button it Up

The last thing our menu needs is a button so we can actually start the game.

  • Add an empty GameObject to Hierarchy, and rename it to ‘StartButton’.
  • Drag StartButton into Canvas to make it Canvas’s child.
  • Select StartButton in Hierarchy.
  • Add a UI Button component in the Inspector.
  • Add a UI Image component in the Inspector.
  • In the Inspector, drag-and-drop the Image component onto the Target Graphic field in the Button (Script) component (this makes the image the button image).
  • Now with StartButton selected in the Hierarchy, move its transform to a position below the game title and byline texts.
  • Add the PlaySprite sprite to the Image(Script) component, then use the Rect Transform (at the top of the Inspector) to resize the button to a nice size and centre it on the X-axis (this sprite is in the project resources download from Part 1).

Here’s what my main menu screen now looks like:


That button might look incredible J, but it doesn’t actually do anything when clicked yet. In the Inspector you’ll notice that the Button (Script) component has an On Click() event list:



This list will hold the action(s) to perform when the button is clicked.

  • Click the little plus sign at the bottom of the On Click() event list. You should see this:


  • Drag-and-drop the Button (Script) component onto the blank field that currently says None (Object):


We also need to perform a specific action when this button is clicked, but we need to script that action first.

You should know how to create a script now (if you can’t remember refer back to earlier parts of this tutorial), so I won’t go into detail:

  • Create a new C# script called ‘MenuButtonScript’ and attach it to the StartButton GameObject.

Here is the entire code for MenuButtonScript:

using UnityEngine;
using System.Collections;

public class MenuButtonScript : MonoBehaviour {

    public void OnClicked()
        Application.LoadLevel ("Scene1");


As you can see from the above code, the line Application.Loadlevel(“Scene1”) loads our main game scene, and that’s how you change scenes in Unity via a script. Quite simple.

Lastly, back in the button’s events list you can now choose which function to call when the button is clicked using the drop-down selector in the On Click() area in the Inspector. Select Menu Button Script > OnClicked(). Now the OnClicked() method in MenuButtonScript will be called every time the button is clicked.

Hit Play to launch the game.


We’ve come a long way since we created a stationary ball in Part 1!

We’ve now used Unity 4.6’s new GUI features to add a score display to our game, and build a menu screen. We also learned a bit more about scenes, including how to navigate between scenes via the Application.Loadlevel() method.

We’ve now created a (mostly) full game. We’ve learned a huge amount over these eight tutorials about how to use Unity. Everything you’ve learned so far can be used as the building blocks of more complex games. You’re probably already getting ideas about how these building blocks are used in your favourite games and how you can use them in your next project!

This isn’t the end of the Pong tutorials.

Continue to Part 9, where we’ll add further polish to the game and tweak it into a masterpiece!