Making lives in Block Breaker

I’m having a hell of a time these past few days trying to get it to work and the major problems are so follows:

  1. GUIText only shows text when I place it on my background and not the lose collider, where the lives should go considering the only way to lose them is when the ball hits it.

  2. Due to the above issue, I placed the GUIText on the background and tried to use a script to modify it. I found my code worked to modify the text, but had no idea how to reference the LoseCollider so I moved the code to there, but despite referencing the GUIText, it doesn’t alter the text. Even exposing it publically, it refuses to do so.

Here’s my code below, I’ve been searching for answers for the past two days and I think I’m onto something, but with so many similar questions being answered in different versions it’s impossible to know if it works or not. I’ll post both the LoseCollider and the Player Lives script I was using on the Background and the LevelManager for any suggestions of if that needs altering too and yet I know the code is out of date, but when I changed it, it messed up the LoseCollider and I couldn’t fix it no matter what:

LoseCollider:

using UnityEngine;
using System.Collections;
using System;

public class LoseCollider : MonoBehaviour {

private LevelManager levelManager;
public GUIText livesGui;
private int Lives;

void start() {
    livesGui = GetComponent<GUIText>();
    livesGui = FindObjectOfType<GUIText>();
    Lives = 5;
}
void OnTriggerEnter2D(Collider2D trigger) {
}
void OnCollisionEnter2D(Collision2D collision){
    levelManager = FindObjectOfType<LevelManager>();
    levelManager.LoadLevel("Lose");
}
void update()
  {
    livesGui.text = "lives x " + Lives;
  }
}

PlayerLivers:

using UnityEngine;
using System.Collections;

public class PlayerLives : MonoBehaviour {

public GUIText livesRemaining;
private LevelManager levelManager;
private LoseCollider loseCollider;
private int Lives;

void Start () {
    levelManager = FindObjectOfType<LevelManager>();
    loseCollider = FindObjectOfType<LoseCollider>();
    livesRemaining = FindObjectOfType<GUIText>();
    Lives = 5;
}

void Update () {
    livesRemaining.text = "lives x " + Lives;
}
}

LevelManager:
using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

public void LoadLevel(string name){
    Brick.breakableCount = 0;
    Application.LoadLevel(name);
}
public void QuitRequest(){
    Application.Quit();
}
public void LoadNextLevel() {
    Brick.breakableCount = 0;
    Application.LoadLevel(Application.loadedLevel + 1);
}
public void BrickDestroys()
{
    if (Brick.breakableCount <= 0) {
        LoadNextLevel();
    }
   }
 }

As you can see I did do some research and using my own deduction to come up with certain ways of how to tackle it (had to look up how to activate the GUIText’s text) but I’ve now hit a brick wall. Any help would be appreciated Thanks.

Which version of Unity are you using currently for this @Aron_Marczylo?

Version 5.0.2f it says when I right-click the icon in the taskbar. I’m on Windows 7.

Ok…

Firstly, I’m pretty sure in the video for the lecture for this game it did mention not to use GUI Text as this was now legacy?

Secondly, if you are using Unity5, it is definitely legacy.

More than happy to help you get this working, but in order to do so we will need to roll back a few steps, if you are prepared to do that I will happily help you :slight_smile:

Well I finished the lectures. I just wanted to finish the game by adding lives, a lives counter and two different game-types too.

That reminds me of something I’m going to experiment with, but hope to get this sorted out first.

Also I don’t ever remember GUI being mentioned in any capacity on any parts of the video so I don’t know what you mean by the video saying NOT to use it…

Legacy, huh? Meaning?

Were you not referring to this?
https://docs.unity3d.com/Manual/class-GUIText.html

You could use a canvas and then a UI Text object in your scene. There’s an example here:

I would probably consider moving the functionality that updates the text for the lives to a game controller, the level manager is kind of acting as this at the moment. From your OnTriggerEnter2D(Collider2D collider) method within the LoseCollider script you could make a call to a method within the game controller, for example;

gameController.PlayerLosesLife();

The gameController in this case would be responsible for then deducting the life and updating the display, it would have the reference to the UI text object for the player lives and nothing else.

This would give you the ability to cleanly then add code later to increase the player lives, perhaps when they get to a specific level, or reach so many points or break a power-up block that gives them a life etc etc. This functionality would also live in the gameController.

@Rob yes, that’s what I’m referring to.

I tried using a UI Text object, however when I shrunk it down even with the Font at the size of 1 was far too big for it to fit in the box and it wouldn’t go below the size of 1. Unless I’m missing something, that should make it impossible to use it as the words would then be covering the entire screen.

If that wasn’t the case I would’ve started out with a UI text object because that’s far more simple and we’ve already done that in the course so far.

That being said, I just attempted it and now I’ve made it large enough, but it doesn’t show up on the game screen at all as if it doesn’t exist. Plus the fond is really faded and unclear so this doesn’t work either.

This the type of thing you are aiming for right?

Game view:

Scene view:

UI Text Properties:

This is on a canvas on a playable scene, e.g. one with some blocks on. It currently appears top left.

Just trying to understand what you are testing…

So when you say “…and now I’ve made it large enough” you are referring to resizing the UI Text object yes?

…and when you say “…but it doesn’t show up on the game screen at all as if it doesn’t exist” do you mean that it isn’t appearing on a scene where you have blocks, but it does appear somewhere else, like perhaps the Start scene?

@Rob

That’s what I want to accomplish yes, however it doesn’t show up at all in the game view no matter where I place it on the scene.

the “…and now I’ve made it large enough” is reference to a issue I originally had with the UI where the text never appeared, but now it does in the UI Text object.

And what I mean is it won’t show up on the game scene where the blocks are at all. It’s as if it doesn’t exist.

Can you pop a screenshot up of the editor with the Scene that has the UI.Text object on it, and have it selected so that the properties show in the inspector.

Yup, here we go:

Also I’ve altered the Z position and no effect if I add to subtract from it.

Looking for some differences between the properties I posted and the ones you have here…

  • no anchors
  • scale looks pretty small - less than a 10th on Y
  • font size is only 6

Here’s a screenshot of the properties from the canvas, what have you got set for Render Mode?

@Rob

After setting the Canvas to what you had there I deleted the previous Text UI (as it became impossible to view) and built a new one in the Canvas which is working perfectly. Now all I need to do is to get the lives to work on it…

1 Like

Great! So, was it the Render Mode that was different?

Need a hand with the next bit or are you ok with that?

@Rob

Yeah the Canvas was at default so once it was change to camra that cleared everything up once I added a new Text UI to it.

As I attempt to alter this, I will certainly need some new help with it. Currently I’m attempting to get this to work in the LoseCollider so I can use a code I found useful (adding an If statement to the collision with if lives <= 0 that I found here ) but I need to somehow expose it on the script and then display what the lives are currently. Hopefully after I prefab all this and add it to the existing game scene it works out great so I can start building more scenes and experiment a bit more.

Assuming levelManager is effectively the game controller, add a variable to that to store the number of lives the player has.

Set it to the default value in the Start() method (for now).

Try having a method on the levelManager which is responsible for just updating the text on the UI.Text object to the player’s current lives, whatever that is (regardless of the event).

Add another method which will reduce the players live by 1 and makes a call to the above method.

You can then call this from the LoseCollider.

@Rob

In the morning and after work I’ll try your method, but for the moment I’ll head to bed. Had a long day of trying to get this to work before I caved and finally asked for help.

1 Like

No worries, well done for the perseverance @Aron_Marczylo :slight_smile: Look forward to seeing it up and running - take it steady :slight_smile:

@Rob

Right, I’ve taken the time and I still haven’t quite figured out how to reference the object the script isn’t attached to and when I attached a script to it, I forgot that it’s attached to both the start and return to menu buttons so they were all changed to so I hit another dead-end down that route.

I’d continue to try .GetComponent<>(), .FindObjectOfType<>() and every other function I could think of in somekind of attempt to grab that text and shove in the code needed to count, not to mention my progress on attempting to bring in the lose collider has also hit a dead-end in my attempts to try and reference the collision mechanic, but every attempt I have at referencing collision 2D or collision just gives me no possible answer and I feel like I’ve tried everything here.

Here’s what I have with LevelManager, I deleted the different lines of code that didn’t work, but I hope you can come up with something so I can finally get this small detail of the game finished and continue building it:

using UnityEngine;
using System.Collections;
using UnityEditor.SceneManagement;

public class LevelManager : MonoBehaviour {
private int Lives;
private LoseCollider loseCollider;
public GameObject playerLives;

void Start(){
    loseCollider = FindObjectOfType<LoseCollider>();
    Lives = 5;
}

public void LoadLevel(string name){
    Brick.breakableCount = 0;
    Application.LoadLevel(name);
}
public void QuitRequest(){
    Application.Quit();
}
public void LoadNextLevel() {
    Brick.breakableCount = 0;
    Application.LoadLevel(Application.loadedLevel + 1);
}
public void BrickDestroys()
{
    if (Brick.breakableCount <= 0) {
        LoadNextLevel();
    }
}
public void LivesCount(){
    if (Lives <= 0) {
        Application.LoadLevel("Lose");
    }
 }
}

The text UI is also named PlayerLives for reference.

Hi @Aron_Marczylo, nice to hear from you again, sounds like you’ve been busy :slight_smile:

Can you pop up all of the scripts you currently have, e.g. how they look at the moment, I was going to scroll up and review them from your other posts but it occurred to me that they may have changed between then and now and I don’t want to derail you etc.

Ideally, if you can just edit the post above this one and include them there that would be great.

Privacy & Terms