Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Secret text doesn't re-activate

Discussion in 'Scripting' started by DustyShinigami, Jun 17, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I'm unsure how to get my secret text to re-activate each time I pick up a secret. It works the first time, but if I pick up any other secrets, the text doesn't display. It keeps track of how many I have though. Looking at the code, I don't see anything that could be conflicting...? Each secret has a SecretsPickup script that gives them a value of 1. That then calls the function to add it to the total and update the text. When a secret is picked up, a bool is set to true in the Update function, which then activates the text, a timer runs, and when it reaches 0, it deactivates. Even during gameplay, if I click on the component to turn it back on, it doesn't. :-\

    Or is there something with the timer that I'm overlooking that prevents it for some reason?

    ChestScript:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Chest : MonoBehaviour
    7. {
    8.     public GameObject chest;
    9.     public static bool allowOpen;
    10.     public GameObject blueSecret;
    11.  
    12.     private Animator chestAnim;
    13.     private SecretsPickup pickupSecret;
    14.  
    15.     void Awake()
    16.     {
    17.         chestAnim = chest.GetComponent<Animator>();
    18.         pickupSecret = blueSecret.GetComponent<SecretsPickup>();
    19.         blueSecret.SetActive(false);
    20.     }
    21.  
    22.     public void OnTriggerEnter(Collider other)
    23.     {
    24.         if (other.gameObject.CompareTag("Player"))
    25.         {
    26.             allowOpen = true;
    27.         }
    28.     }
    29.  
    30.     public void ChestOpen()
    31.     {
    32.         if (allowOpen)
    33.         {
    34.             chestAnim.SetBool("canOpen", true);
    35.             chestAnim.SetTrigger("open");
    36.             blueSecret.SetActive(true);
    37.             pickupSecret.Secret();
    38.         }
    39.     }
    40. }
    SecretsPickup Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SecretsPickup : MonoBehaviour
    6. {
    7.     public int value;
    8.  
    9.     private Animator secretAnim;
    10.  
    11.     void Awake()
    12.     {
    13.         secretAnim = GetComponent<Animator>();
    14.     }
    15.  
    16.     public void OnTriggerEnter(Collider other)
    17.     {
    18.         if (other.tag == "Player")
    19.         {
    20.             FindObjectOfType<GameManager>().AddSecret(value);
    21.             Destroy(gameObject);
    22.         }
    23.     }
    24.  
    25.     public void Secret()
    26.     {
    27.         StartCoroutine("SecretRevealed");
    28.     }
    29.  
    30.     public IEnumerator SecretRevealed()
    31.     {
    32.         yield return new WaitForSeconds(.5f);
    33.         secretAnim.SetTrigger("secretMove");
    34.     }
    35. }
    36.  
    GameManager:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class GameManager : MonoBehaviour
    8. {
    9.  
    10.     public static int currentEmbers;
    11.     public static int currentSecrets;
    12.     public TextMeshProUGUI emberText;
    13.     public TextMeshProUGUI secretsText;
    14.  
    15.     private bool secretCollected = false;
    16.  
    17.     public float timer = 2f;
    18.  
    19.     void Update()
    20.     {
    21.         if (secretCollected)
    22.         {
    23.             secretsText.enabled = true;
    24.             timer -= Time.deltaTime;
    25.             if (timer <= 0f)
    26.             {
    27.                 //Time.timeScale = 0;
    28.                 secretsText.enabled = false;
    29.                 //secretCollected = false;
    30.             }
    31.         }
    32.     }
    33.  
    34.     public void AddEmber(int embertoAdd)
    35.     {
    36.         currentEmbers += embertoAdd;
    37.         emberText.text = "Embers: " + currentEmbers;
    38.         SetCountText();
    39.     }
    40.  
    41.     public void SetCountText()
    42.     {
    43.         emberText.text = "Embers: " + currentEmbers.ToString();
    44.     }
    45.  
    46.     public void SetSecondCountText()
    47.     {
    48.         secretsText.text = "Secrets Found: " + currentSecrets.ToString();
    49.     }
    50.  
    51.     public void AddSecret(int secrettoAdd)
    52.     {
    53.         secretCollected = true;
    54.         currentSecrets += secrettoAdd;
    55.         secretsText.text = "Secrets Found: " + currentSecrets;
    56.         SetSecondCountText();
    57.     }
    58. }
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    In your GameManager, the update loop is always turning it off, once the timer hits zero. I think you just need to reset the timer and unset the secretCollected variable.
    Code (csharp):
    1. void Update()
    2. {
    3.     if (secretCollected)
    4.     {
    5.         secretsText.enabled = true;
    6.         timer -= Time.deltaTime;
    7.         if (timer <= 0f)
    8.         {
    9.             secretsText.enabled = false;
    10.             secretCollected = false;
    11.             timer = 2.0f; // or whatever starting value
    12.         }
    13.     }
    14. }
    Otherwise, if secretCollected is true and timer isn't reset, it will instantly disable the text again.
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Okay, thanks. Now that I know where I need focus, I'll try experimenting with the code. :)
     
  4. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    So, doesn't the Update function run from the start more than once? I would have thought it would check through the code from the start every frame?
     
  5. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Fixed it. :D
     
    WallaceT_MFM likes this.