Search Unity

Resolved Weird Text Glitch/Bug?

Discussion in 'C# Job System' started by Byte-Blasted, May 27, 2023.

  1. Byte-Blasted

    Byte-Blasted

    Joined:
    Dec 2, 2021
    Posts:
    2
    I didn't know if to post this in the UI forum instead, but I decided to post it here because it might be something with the script. (First time ever posting a problem on a forum) I have an extremely weird bug that I cant find anywhere else where my text only updates for 1 frame, and then unity says that it can't find it anymore.
    (This is code for when my weapon updates the text so it can display if you shot/reloaded and etc.)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    using StarterAssets;
    public class BlockShotAnims : MonoBehaviour
    {
    public int currentAmmo = 1;
    public int reserveAmmo = 20;
    public int maxAmmo = 1;

    //doing the same script with TextMeshPro gave the exact same results.
    public Text cAmmo;
    public Text rAmmo;
    public Text mAmmo;

    void Update()
    {
    cAmmo.text = "" + currentAmmo;
    rAmmo.text = "" + reserveAmmo;
    mAmmo.text = "" + maxAmmo;
    }
    }

    //raw file shown here:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    using StarterAssets;
    public class BlockShotAnims : MonoBehaviour
    {
    private Animator animan;
    public bool isReloading = false;
    public bool isShooting = false;
    public bool isWalking = false;
    public bool isInspecting = false;
    public int currentAmmo = 1;
    public int reserveAmmo = 1;
    public int maxAmmo = 1;
    public Text cAmmo;
    public Text rAmmo;
    public Text mAmmo;
    // Start is called before the first frame update
    void Start()
    {
    animan = gameObject.GetComponent<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
    cAmmo.text = "" + currentAmmo;
    rAmmo.text = "" + reserveAmmo;
    mAmmo.text = "" + maxAmmo;
    //priority 1
    if (Input.GetButtonDown("mouse0") && !isReloading && !isShooting && currentAmmo > 0)
    {
    StopAllCoroutines();
    isInspecting = false;
    isWalking = false;
    isShooting = true;
    animan.SetBool("Inspecting", false);
    animan.SetBool("InspectingShot", false);
    animan.SetBool("Walking", false);
    StartCoroutine(shootAnim());
    }
    //priority 2
    if(Input.GetButtonDown("R") && !isReloading && !isShooting)
    {
    StopAllCoroutines();
    isInspecting = false;
    isWalking = false;
    isReloading = true;
    animan.SetBool("Inspecting", false);
    animan.SetBool("InspectingShot", false);
    animan.SetBool("Walking", false);
    StartCoroutine(reloadAnim());
    }
    //priority 3
    if (Input.GetButtonDown("Q") && !isReloading && !isShooting && !isInspecting)
    {
    animan.SetBool("Walking", false);
    StopAllCoroutines();
    isWalking = false;
    isInspecting = true;
    StartCoroutine(inspectAnim());
    }
    if(FirstPersonController._speed > 0f)
    {
    animan.SetBool("Walking", true);
    }else if(FirstPersonController._speed <= 0)
    {
    animan.SetBool("Walking", false);
    }
    }
    IEnumerator reloadAnim()
    {
    animan.SetBool("Reload", true);
    yield return new WaitForSeconds(3);
    animan.SetBool("Reload", false);
    if(currentAmmo < maxAmmo)
    {
    reserveAmmo -= maxAmmo;
    currentAmmo = maxAmmo;
    }
    isReloading = false;
    }
    IEnumerator shootAnim()
    {
    currentAmmo -= 1;
    animan.SetBool("Shoot", true);
    yield return new WaitForSeconds(.75f);
    animan.SetBool("Shoot", false);
    isShooting = false;
    }
    IEnumerator inspectAnim()
    {
    if(currentAmmo == 0)
    {
    animan.SetBool("InspectingShot", true);
    yield return new WaitForSeconds(6.5f);
    animan.SetBool("InspectingShot", false);
    isInspecting = false;
    }else if(currentAmmo > 0)
    {
    animan.SetBool("Inspecting", true);
    yield return new WaitForSeconds(6.5f);
    animan.SetBool("Inspecting", false);
    isInspecting = false;
    }
    }
    }


    Even though the proper GameObjects are put into the script, it still says it cant be found and that theres an error on the first line that an ammo.text pops up.
    If someone could help a novice programmer out that would be greatly appreciated.
     

    Attached Files:

  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,956
    The error is in line 30 of that script. Check which object instance is affected by this, possibly one of the three c/r/mAmmo types. Then check when and where you assign them by a script, or destroy them, or destroy one of their parent objects.

    This would also be a good situation to learn about the debugger, how to set breakpoints and step through code. ;)

    Also naming is very important because right now it‘ll be super easy to mistake mAmmo for rAmmo. These are labels, so call them currentAmmoLabel for instance.
     
    Byte-Blasted likes this.
  3. Byte-Blasted

    Byte-Blasted

    Joined:
    Dec 2, 2021
    Posts:
    2
    Hey there, I used a work around where the ammo is counted in a separate script with the text as opposed to the animations and ammo in one. (Took note of the advise above concerning confusing names as well)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    public class BlocketLauncherAmmo : MonoBehaviour
    {
    public static int currentAmmo = 0;
    public static int reserveAmmo = 15;
    public static int maxAmmo = 1;
    public TMP_Text currentAmmoText;
    public TMP_Text reserveAmmoText;
    public TMP_Text maxAmmoText;
    //add a public void changeAmmo later so you don't waste memory updating the text when you don't need to
    void Update()
    {
    currentAmmoText.text = "" + currentAmmo;
    reserveAmmoText.text = "" + reserveAmmo;
    maxAmmoText.text = "" + maxAmmo;
    }
    }

    //updated 2nd script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    using StarterAssets;
    public class BlocketLauncherAnims : MonoBehaviour
    {
    private Animator animan;
    public bool isReloading = false;
    public bool isShooting = false;
    public bool isWalking = false;
    public bool isInspecting = false;
    // Start is called before the first frame update
    void Start()
    {
    animan = gameObject.GetComponent<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
    //priority 1
    if (Input.GetButtonDown("mouse0") && !isReloading && !isShooting && BlockShotAmmo.currentAmmo > 0)
    {
    StopAllCoroutines();
    isInspecting = false;
    isWalking = false;
    isShooting = true;
    animan.SetBool("Inspecting", false);
    animan.SetBool("InspectingShot", false);
    animan.SetBool("Walking", false);
    StartCoroutine(shootAnim());
    }
    //priority 2
    if(Input.GetButtonDown("R") && !isReloading && !isShooting)
    {
    StopAllCoroutines();
    isInspecting = false;
    isWalking = false;
    isReloading = true;
    animan.SetBool("Inspecting", false);
    animan.SetBool("InspectingShot", false);
    animan.SetBool("Walking", false);
    StartCoroutine(reloadAnim());
    }
    //priority 3
    if (Input.GetButtonDown("Q") && !isReloading && !isShooting && !isInspecting)
    {
    animan.SetBool("Walking", false);
    StopAllCoroutines();
    isWalking = false;
    isInspecting = true;
    StartCoroutine(inspectAnim());
    }
    if(FirstPersonController._speed > 0f)
    {
    animan.SetBool("Walking", true);
    }else if(FirstPersonController._speed <= 0)
    {
    animan.SetBool("Walking", false);
    }
    }
    IEnumerator reloadAnim()
    {
    animan.SetBool("Reload", true);
    yield return new WaitForSeconds(3);
    animan.SetBool("Reload", false);
    if(BlockShotAmmo.currentAmmo < BlockShotAmmo.maxAmmo)
    {
    BlockShotAmmo.reserveAmmo -= BlockShotAmmo.maxAmmo;
    BlockShotAmmo.currentAmmo = BlockShotAmmo.maxAmmo;
    }
    isReloading = false;
    }
    IEnumerator shootAnim()
    {
    BlockShotAmmo.currentAmmo -= 1;
    animan.SetBool("Shoot", true);
    yield return new WaitForSeconds(.75f);
    animan.SetBool("Shoot", false);
    isShooting = false;
    }
    IEnumerator inspectAnim()
    {
    if(BlockShotAmmo.currentAmmo == 0)
    {
    animan.SetBool("InspectingShot", true);
    yield return new WaitForSeconds(6.5f);
    animan.SetBool("InspectingShot", false);
    isInspecting = false;
    }else if(BlockShotAmmo.currentAmmo > 0)
    {
    animan.SetBool("Inspecting", true);
    yield return new WaitForSeconds(6.5f);
    animan.SetBool("Inspecting", false);
    isInspecting = false;
    }
    }
    }


    Then everything seemed to work perfectly with no errors!
    Thanks a lot!