Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to show a text under a condtion. Please help.

Discussion in 'Scripting' started by Moortje, Jun 24, 2022.

  1. Moortje

    Moortje

    Joined:
    Jun 12, 2022
    Posts:
    3
    Hi.
    I'm very new to Unity and trying to create a clone game of Flappy Bird.
    I'm almost all done and have this one thing left which is to show a text "Got ya" (A joke to my friends who will test-play it) on GameOver Scene when they reach the Best Score of 25.

    I've been at it forr about five hours and my head is about to explode.

    The text, Got ya, currently shows on GameOver Scene.
    I want to hide it until the best score is >= 25.
    I can't get it to hide.

    How would I go about it?
    Please and thank you!
     
    Last edited: Jun 25, 2022
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    192
    Code (CSharp):
    1.  
    2. public class GotYaManager : MonoBehaviour
    3. {
    4. public Text text;//Very important that this is not on the same gameobject since once this deactivated the update wont be called
    5.  
    6. void Update()
    7. {
    8. text.gameObject.SetActive(ScoreManager.score >= 25);//ScoreManager.score is a static field
    9. }
    10. }
     
    Moortje likes this.
  3. Moortje

    Moortje

    Joined:
    Jun 12, 2022
    Posts:
    3
    Hi. Thank you for your answer. I feel like I'm lost in a desert and your reply means so much more than you'd think.
    I've tried copying and pasting your code and it seems nothing is happening.

    I've set score as a static field by writing
    public static int score = 0;

    I'm not sure what you mean by 'important that this is not on the same gameobject'.
    What shouldn't be on the same gameobject as what..?

    Also, I'm not sure why this doesn't work, and if you could offer some thoughts on what should be changed, it'd be very appreciated.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Gotya : MonoBehaviour
    7. {
    8.     public Text text;//Very important that this is not on the same gameobject since once this deactivated the update wont be called
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.       if (Score.bestScore >= 1)
    14.         {
    15.             text.gameObject.SetActive(true);//ScoreManager.score is a static field
    16.  
    17.         }
    18.       else
    19.             text.gameObject.SetActive(false);//ScoreManager.score is a static field
    20.     }
    I know I'm asking a total stranger for lots of help... but I got nothing else.
    I'm currently studying C How to Program by Deitel, a school recommend that I study this before enrolling.
    But I want to finish this game so that I can 'use' knowledge while learning it.
    Thanks a lot.
     
    Last edited: Jun 25, 2022
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    When posting code to the forum, make sure to use Code Tags.

    Inside of their code, they specifically put the check inside of the
    Update()
    method, to allow it to check the score every frame. Your code has it in the
    Start()
    method, which means it only checks once, at the beginning.

    The code they provided disables the GameObject the Text component is on, so if you put this script on the same object as the Text component, it would disable the GameObject, and would also disable the script as a result.
     
    Moortje and TheFunnySide like this.
  5. Moortje

    Moortje

    Joined:
    Jun 12, 2022
    Posts:
    3
    Thank you! I finally got it to work after thinking about what TheFunnySide and you said.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Gotya : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         GetComponent<Text>().text = "Gotya";
    12.         gameObject.SetActive(Score.bestScore >= 25);
    13.  
    14.     }
    Such a simple code. I didn't put it within update though.
    Figured it only has to check it once. Would it have been better to put it within update?
    Also, thanks for letting me know about Code Tags!
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Some people would have put the Game Over in the same scene, but since you're switching scenes, you only need to check it once at Start, so it's fine.
     
    Moortje likes this.