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. Dismiss Notice

Make game object (Text) dissapear or not visible through other objects

Discussion in 'Scripting' started by kcpilotguy, May 30, 2019.

  1. kcpilotguy

    kcpilotguy

    Joined:
    Feb 6, 2019
    Posts:
    5
    Hi,

    I'm working on a game where I have some floating instructions right as the game starts. Once the FPS player walks through the text and down a hallway, it is visible even after they have turned a corner. I have a complex series of tunnels and the text is visible as long as you are facing it. I have tried using a script to fade it out but i keep getting errors. I'm a total noob so I'm sure it is something simple I'm missing. Any help is appreciated.

    I used a text mesh to make the floating text. And i tried this code to make it dissapear. My game object is called FloatingText1 in case you need to know.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. public class FloatingTextFade : MonoBehaviour
    6. {
    7. // can ignore the update, it's just to make the coroutines get called for example
    8.     void Update()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.Q))
    11.         {
    12.             StartCoroutine(FadeTextToFullAlpha(1f, GetComponent<Text>()));
    13.         }
    14.         if (Input.GetKeyDown(KeyCode.E))
    15.         {
    16.             StartCoroutine(FadeTextToZeroAlpha(1f, GetComponent<Text>()));
    17.         }
    18.     }
    19.     public IEnumerator FadeTextToFullAlpha(float t, Text i)
    20.     {
    21.         i.color = new Color(i.color.r, i.color.g, i.color.b, 0);
    22.         while (i.color.a < 1.0f)
    23.         {
    24.             i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a + (Time.deltaTime / t));
    25.             yield return null;
    26.         }
    27.     }
    28.     public IEnumerator FadeTextToZeroAlpha(float t, Text i)
    29.     {
    30.         i.color = new Color(i.color.r, i.color.g, i.color.b, 1);
    31.         while (i.color.a > 0.0f)
    32.         {
    33.             i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a - (Time.deltaTime / t));
    34.             yield return null;
    35.         }
    36.     }
    37. }
    38.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Always copy and post the error so we get a reference to the line number.

    But if this isn't on the GameObject with the Text component, then GetComponent<Text>() will return null.

    And since you said you used a Text Mesh, then that is the issue. A Text Mesh isn't the same as a Text component.

    And if you're using TextMeshPro that's different also. You have to get the correct component.
     
  3. kcpilotguy

    kcpilotguy

    Joined:
    Feb 6, 2019
    Posts:
    5
    Ok, I used a text mesh...which component should I have used to do this? All I wanted was a set of instructions that said "blah blah blah...go this way and do this..blah blah blah" and then a second set of instructions floating further down the tunnel saying "ok, you did this, now you are ready to do this..."

    I found a tutorial that said text mesh was a way to do that which is why I used that one.

    Any help is appreciated.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    While I suggest TextMeshPro since it's free, it doesn't matter what you used. You just need to get the right component. If you use a TextMesh, you need to get the TextMesh component, not a Text component.
     
  5. kcpilotguy

    kcpilotguy

    Joined:
    Feb 6, 2019
    Posts:
    5
    Ok, so you mean I should change the code to look like this:

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Q))
    2.         {
    3.             StartCoroutine(FadeTextToFullAlpha(1f, GetComponent<TextMesh>()));
    4.         }
    5.         if (Input.GetKeyDown(KeyCode.E))
    6.         {
    7.             StartCoroutine(FadeTextToZeroAlpha(1f, GetComponent<TextMesh>()));
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Yes, and just make sure your methods take a TextMesh as a parameter and not a Text.
     
  7. kcpilotguy

    kcpilotguy

    Joined:
    Feb 6, 2019
    Posts:
    5
    Sounds like a plan...appreciate the help. Will give it a try and see what happens.