-2

My problem is: I play a game , and I want that if you lose , inmediately in the top of the screen must appear a text with "Record: X" , I don't want change scene or something, no, I want only put a text in the top of screen in the same scene, like in Stack

Is possible?

My code for example is a 2 objects with collision, and when they collision, i want put this text in the top.

public class Colision : MonoBehaviour {

    public Text points;
    int contador=0;
    int Veces_Pequeño=0;

    void OnCollisionEnter(Collision col){
        if ( col.gameObject.name == "Cube") {
            col.gameObject.SetActive (false);
        }

        if ( col.gameObject.name == "Cube1") {
            col.gameObject.SetActive (false);
        }
    }
}
Rf Mvs
  • 166
  • 11
  • I want like in STACK! STACK starts with text and just below appear a game with a button and Scene and if you lose, the text "RECORD" inmediately appears in the top. How?? – Rf Mvs Nov 09 '16 at 19:15
  • the language is C# – Rf Mvs Nov 09 '16 at 19:17
  • 2
    You really should watch basic unity tutorials like [roll-a-ball](https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial) it goes over how to show text on screen. – Scott Chamberlain Nov 09 '16 at 23:00

2 Answers2

3

I'm having trouble understanding what you're asking, but if it's as simple as I think it is, make sure points is disabled when starting the game and add this code when you'd like the points to be displayed.

//Set the text to what ever you would like.
points.text = "Record: " + contador;
//Enable the gameobject for it to be seen.
points.gameObject.SetActive(true);
Alox
  • 651
  • 12
  • 24
  • Alright, then I recommend using two text inside a horizontal layout group where the first one has the text "RECORD" with your custom font and the second one is the one connected to the variable points. – Alox Nov 09 '16 at 19:54
  • but how can I do appear this text? Layout is inside in which scene? – Rf Mvs Nov 09 '16 at 20:02
  • Horizontal Layout Group is a [component](https://docs.unity3d.com/Manual/script-HorizontalLayoutGroup.html), and to make it apear you could setActive(true) on both at the same time or have a reference to the parent which you could disable/enable in the same way. – Alox Nov 09 '16 at 20:08
  • @RfMvs, don't forget to accept an answer to close the question if you found what you were looking for. – Alox Nov 25 '16 at 17:49
0

Well, you can just change the text and enabled properties of Text.

I assume you have a GameObject with the Text component and a Canvas...

public class Colision : MonoBehaviour {

    public Text points;
    int contador = 0;
    int Veces_Pequeño = 0;
    public string beforeText = "record: "; //just added this so it is easy to change in unity editor

    void Start(){
        points.enabled = false;
    }

    void OnCollisionEnter(Collision col){
        if ( col.gameObject.name == "Cube") {
            col.gameObject.SetActive (false);
            points.text = beforeText + contador.toString();
            points.enabled = true;
        }

        if ( col.gameObject.name == "Cube1") {
            col.gameObject.SetActive (false);

            points.text = beforeText + contador.toString();
            points.enabled = true;
        }
    }
}

This should help a lot: https://docs.unity3d.com/Manual/script-Text.html

yummypasta
  • 1,398
  • 2
  • 17
  • 36