Search Unity

trying to make a Wave text (GUIText) to appear before each wave and last for only a few seconds

Discussion in 'UGUI & TextMesh Pro' started by newdev4ios, May 24, 2017.

  1. newdev4ios

    newdev4ios

    Joined:
    May 24, 2017
    Posts:
    1
    Im trying to make a Wave count display before each wave of the asteroid shooter (the tutorial game) and then dissappear after a few seconds, i have been able to get the text to appear once for the first game but once it dissappears (i use the destroyByTime script to make it dissappear) it never reappears saying that the gameobject has been destroyed but the thing is i put the GUIText in an empty gameobject naming it wavTXT and moved it to my prefabs folder.

    i dont mind rewriting code

    here is my code for the gameController:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class gameController : MonoBehaviour {
    6.  
    7.     public GameObject hazard;
    8.     public Vector3 spawnVals;
    9.     public int hazCount;
    10.     public float sWait;
    11.     public float startWait;
    12.     private int hazC;
    13.     private int waveCount = 1;
    14.  
    15.     public GUIText waveTXT;
    16.     public GameObject waveUI;
    17.     public GUIText scoreTXT;
    18.     private int score;
    19.  
    20.  
    21.     private void Start()
    22.     {
    23.         score = 0;
    24.         updateScore();
    25.         updateWave();
    26.         StartCoroutine (spawnWaves());
    27.     }
    28.    
    29.  
    30.     IEnumerator spawnWaves()
    31.     {
    32.         hazC = hazCount;
    33.         while (true)
    34.         {
    35.             yield return new WaitForSeconds(startWait);
    36.            
    37.             for (int i = 0; i < hazC; i++)
    38.             {
    39.                 Vector3 spawnPos = new Vector3(Random.Range(-spawnVals.x, spawnVals.x), spawnVals.y, spawnVals.z);
    40.                 Quaternion spawnRot = Quaternion.identity;
    41.                 Instantiate(hazard, spawnPos, spawnRot);
    42.                 yield return new WaitForSeconds(sWait);
    43.             }
    44.             hazC += 2;
    45.             waveCount += 1;
    46.             updateWave();
    47.         }
    48.     }
    49.  
    50.     public void addScore(int newVal)
    51.     {
    52.         score += newVal;
    53.         updateScore();
    54.     }
    55.  
    56.     void updateWave()
    57.     {
    58.         waveTXT.text = "Wave: " + waveCount;
    59.         waveUI = Instantiate(waveUI, Camera.main.WorldToViewportPoint(gameObject.transform.position), Quaternion.identity);
    60.        
    61.     }
    62.  
    63.     void updateScore()
    64.     {
    65.         scoreTXT.text = "Score: " + score;
    66.     }
    67.  
    68.  
    69.  
    70. }
    71.  
    Here is my DestroyByTime code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class timeDestroyer : MonoBehaviour {
    6.     public float lifeTime;
    7.     // Use this for initialization
    8.     void Start () {
    9.         Destroy(gameObject, lifeTime);
    10.     }
    11.    
    12. }
     

    Attached Files:

  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't remember exactly what GUIText is.. normally I'd recommend using Text on a canvas, but that's up to you.
    back to your dilemma -- I'd say make a reference to a script that is on your Text/GUIText.
    On that script, have a function that takes an int as a parameter.
    Code (csharp):
    1.  
    2. public ShowWaveText(int wavenum) {
    3.    gameObject.SetActive(true);
    4.    GetComponent<Text>().text = "Starting wave " + wavenum.ToString();
    5.    Invoke("DisableText", 2);
    6.   }
    7. void DisableText(){
    8.    gameObject.SetActive(false);
    9.    }
    10.  
    So, the script reference.. then when you start the wave, send the wave number to that function, it'll activate the game object and update the text. It will then set a delayed invoke for the next method, which should disable the game object.
    I think that'll work.