Creating text message on start of each level

I want to have a text element at the beginning of each level stating the level the player is about to play, this text element should disappear at the moment the player clicks on the mouse to launch the ball. I have tried several approaches in both the levelmanager and losecollider scripts, but i didnt get it to work. Making the text appear is easy , but I cant find the method to make it go away on the mouseclick. Cant figure out what element and how to call on it to create that function. This is my Losecollider script:
using UnityEngine;
using System.Collections;

public class LoseCollider : MonoBehaviour {

public static int numLives;

public static bool firstLevelLoaded = true;
private LevelManager levelManager;

private void Start() {
    if (firstLevelLoaded) {
        numLives = 5;
        firstLevelLoaded = false;
    }
}

public void OnTriggerEnter2D(Collider2D trigger) {
      levelManager = GameObject.FindObjectOfType<LevelManager>();
    numLives--;  

    if (numLives < 0) {
        levelManager.LoadLevel("Lose");
        firstLevelLoaded = true;
    }else {
        GameplayController.instance.Countlives(numLives);
        Ball.instance.SetBallPosition();
    }

   
    
}
	
	
    public void OnCollisionEnter2D (Collision2D collision) {
	
}

}
and this is my Levelmanager script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour {

public void LoadLevel(string name){
	Brick.breakableCount = 0;
	SceneManager.LoadScene(name);
}

public void QuitRequest(){
	Debug.Log("I want to quit");
	Application.Quit ();
}

public void LoadNextLevel() {
    Brick.breakableCount = 0;
    SceneManager.LoadScene(sceneBuildIndex: Application.loadedLevel + 1);
    }

public void BrickDestroyed() {
	if (Brick.breakableCount <= 0) {
        LoadNextLevel();
	}
}

}
All tips and trics are welcome.
Thanks.

Hi,

One approach could be to use the SetActive method on the GameObject you want to show/hide. It takes a boolean parameter(true/false).

So, when the scene loads, itā€™s active. You are already checking for a mouse click to launch the ball, so then just add a line to set the UI Text GameObject as inactive, something like;

Class level variable to pass a reference to your UI Text Game Object into

public Text theTextToToggle;

A check in the Start method just to make sure that the UI Text GameObject to toggle is actually active

private void Start()
{
    // is the toggle text's GameObject active, if not, set it to active
    if(!theTextToToggle.gameObject.activeSelf)
    {
        theTextToToggle.gameObject.SetActive(true);
    }
}

In the mouse click detection

if(Input.GetMouseKeyDown(0))
{
    // set text to toggle's GameObject as inactive
    textToToggle.gameObject.SetActive(false);

    // ...
}

Note, after you get this working, you may then want to give a little bit of additional thought as to where this functionality should live.

Hope this helps :slight_smile:


See also;

Thanks, iā€™m going to try it first thing tomorrow, and let you know if it worked.

1 Like

Youā€™re more than welcome.

As a final suggestion, its often useful to create/test things like this in a new scene or two, iron out any issues, then integrate into what you already know works. :slight_smile:

I cant work it out, this is what i have done in the script Balls:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Ball : MonoBehaviour {

    public AudioClip boing;
    public static Ball instance;
    public Text LevelText;

  
    private Paddle paddle;
    private bool hasStarted = false;
    private Vector3 paddleToBallVector;
    private bool firstBall = true;

    // Use this for initialization
    void Start() {
        paddle = GameObject.FindObjectOfType<Paddle>();
        paddleToBallVector = this.transform.position - paddle.transform.position;
        
        MakeInstance();
        SetBallPosition();
        if (!LevelText.activeSelf) {
            LevelText.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update() {
        if (!hasStarted) {
            //Lock the ball relative to the paddle.
            this.transform.position = paddle.transform.position + paddleToBallVector;

            //Waiting for a mouseclick to launch ball.
            if (Input.GetMouseButtonDown(0)) {
                hasStarted = true;
                this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
                LevelText.SetActive(false);
            }
        }
    }

    void OnCollisionEnter2D(Collision2D collision) {
        Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));

    }

    void MakeInstance() {
        if (instance == null) {
            instance = this;
        }
    }

    public void SetBallPosition() {
        if (firstBall) {
            paddleToBallVector = this.transform.position - paddle.transform.position;
            firstBall = false;
        }
        hasStarted = false;
    }
}

And then i get these messages:

1 Like

My badā€¦

These are properties and methods of a GameObject, so we need to use the following when they are used;

private void Start()
{
    // is the toggle text active, if not, set it to active
    if(!theTextToToggle.gameObject.activeSelf)
    {
        theTextToToggle.gameObject.SetActive(true);
    }
}

ā€¦andā€¦

if(Input.GetMouseKeyDown(0))
{
    // set text to toggle as inactive
    textToToggle.gameObject.SetActive(false);

    // ...
}

ā€¦thus referring to the UI Textā€™s GameObject.

Iā€™ll update my example above to correct this also.

Thanks for the tips, I can play the level now but I get other error messages and the text does not dissappear.
Did I do the right thing by placing this code in my Balls script? Or do I need to declair the boolean?
Because when I try that I get error that there already is a statement for LevelText.
Or do I need to place the second if function somewhere else?
This is what I did in the Balls script:
public class Ball : MonoBehaviour {

public AudioClip boing;
public static Ball instance;
public Text LevelText;


private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
private bool firstBall = true;

// Use this for initialization
void Start() {
    paddle = GameObject.FindObjectOfType<Paddle>();
    paddleToBallVector = this.transform.position - paddle.transform.position;
    
    MakeInstance();
    SetBallPosition();
    if (!LevelText.gameObject.activeSelf) {
        LevelText.gameObject.SetActive(true);
    }
}

// Update is called once per frame
void Update() {
    if (!hasStarted) {
        //Lock the ball relative to the paddle.
        this.transform.position = paddle.transform.position + paddleToBallVector;

        //Waiting for a mouseclick to launch ball.
        if (Input.GetMouseButtonDown(0)) {
            hasStarted = true;
            this.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
            LevelText.gameObject.SetActive(false);
        }
    }
}

void OnCollisionEnter2D(Collision2D collision) {
    Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));

}

void MakeInstance() {
    if (instance == null) {
        instance = this;
    }
}

public void SetBallPosition() {
    if (firstBall) {
        paddleToBallVector = this.transform.position - paddle.transform.position;
        firstBall = false;
    }
    hasStarted = false;
}

}

And these are the error messages:
error_messages2
Thanks again for spending time on this, maybe its a bit to complicated for me, I just started this course.

Hi Rudi,

Can you zip up the project files for me and give me the link to the download, I will happily take a look for you.

Also, which version of Unity are you using?

I am using Unity 2017.2 of3 personal, where do you want me to put the zip file?

1 Like

Ok, great.

If itā€™s larger than 10mb, if you could use something like Google Drive or DropBox and then just give me the URL here, alternatively, if its 10mb or less, you can upload the .zip file into your reply.

Looks like the issue will be my bad by the way, given you some wrong info I think - but if I can see it all in context I will resolve for you :slight_smile:

No problem, I will atta&che the zip file in here, you only need the asstes folder dont you?
Thanks for trying to solve this for me.

Assets.zip (2.4 MB)

Can you give me the whole set of project files, I should be able to just open the project then, rather than having to create a new one at this end. Thanks :slight_smile:


Updated Wed Nov 29 2017 21:40

Donā€™t worry, Iā€™m on itā€¦


Updated Wed Nov 29 2017 21:42

Ok, so Iā€™m looking at your Level 1 scene, and the Ball in the Hierarchy, and the Prefab, donā€™t have the LevelText populated with the Level Text UI GameObject from the scene.

Here is the link to my drive:
https://drive.google.com/file/d/1sru1MXgLxZepQyTxe7GMEyh_QckJAsiw/view?usp=sharing

1 Like

See the update above - we overlapped :slight_smile:

Dont understand what that means, how do I populate LevelText with that UI GameObject?

You just drag it from the Hierarchy into the exposed field on the, in your case, Ball script component.

Iā€™ve just done that, and itā€™s removed one of the errors, give me a sec and Iā€™ll sort the second tooā€¦

Do I need to add a component to the LevelText?

Nopeā€¦ the opposite actuallyā€¦

Ok, to run through thenā€¦

So, first of all, open your Level 1 scene.

In the Hierarchy, select your Ball.

image

In the Inspector, you will see it has an exposed field called Level Text, this is your public Text LevelText variable in your Ball.cs script.

image

Drag the LevelText GameObject from the Hierarchy into this field in the Ballā€™s Level Text field.

image

image

Second, you have also attached the Ball.cs script to the Level Text GameObject in the Hierarchy (and the prefab etc), you donā€™t need this, so remove this component.

image

image

Run the game, click start, ā€œLevel 1ā€ will appear at the top of the screen and then disappears when you click the mouse button.

You will get an error on Level 2 because we havenā€™t repeated the steps yet - however - there is going to be a better way to do this rather than you having to repeat these steps on all 10 levels. But, before we do that, follow the above steps and confirm that you can get the first level to do as you expect first - I will be waiting :slight_smile:

I have done those things and now I dont get any errors, but the text doenst appear.
Here is a screenshot from my Ball gameobject:

And here is the LevelText:

So, I probably did something wrong, but there is no text appearing not when I start from the ā€˜startā€™ level or from the ā€˜level_01ā€™

1 Like

But as I am watching here, the position is completly off I think

1 Like

Privacy & Terms