Search Unity

Bug Issue with multiple objects with the same scripts

Discussion in 'Scripting' started by Adastour, Aug 23, 2021.

  1. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    Hello

    I am working on a zombies project and I was adding wall weapons and discovered this bug where multiple wall weapons with the same script don't work. The first prefab instance works; however, the second instance doesn't show the text but lets me buy it.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;

    public class BuyWallWeapon : MonoBehaviour
    {
    private WeaponManager weaponManager;

    public int index;

    public string weaponName;

    public GameObject buyGunUIParent;

    // Start is called before the first frame update
    void Start()
    {
    weaponManager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<WeaponManager>();
    }

    private bool RangeOfPlayer = false;

    private void OnTriggerEnter(Collider other)
    {
    if(other.tag.Equals("Player"))
    {
    TextMeshProUGUI[] gunBuyTxts = buyGunUIParent.GetComponentsInChildren<TextMeshProUGUI>();
    foreach(TextMeshProUGUI gunBuyTxt in gunBuyTxts)
    {
    gunBuyTxt.text = "Push F to buy " + weaponName + " for " + weaponManager.weaponCost[index] + " Points";
    }
    RangeOfPlayer = true;
    }
    }

    private void OnTriggerExit(Collider other)
    {
    if(other.tag.Equals("Player"))
    {
    RangeOfPlayer = false;
    }
    }

    // Update is called once per frame
    void Update()
    {
    if (RangeOfPlayer)
    {
    buyGunUIParent.SetActive(true);
    if (Input.GetKeyDown(KeyCode.F))
    BuyWeapon(index);
    } else
    {
    buyGunUIParent.SetActive(false);
    }
    }

    void BuyWeapon(int index)
    {
    weaponManager.ChooseWeapon(index);
    }
    }

     
  2. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    Shall I submit a video of the problem to help further understanding of the bug?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Never hurts.

    What is buyGunUIParent?
     
  4. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    The buyGunUIParent is the GameObject that parents the main text and the text shadow.
     
  5. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    I'll send a video link of the issue
     
  6. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
  7. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    And, side note, under debug mode, whenever I enter the box collider's trigger, the script shows I'm in range and lets me buy the item I'm next to but doesn't show the text.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It's a good idea when doing clips to show the inspector also and what is suppose to change, what the script is targeting and such.

    My first thought is you aren't targeting the right object. Normal debugging steps suggest checking if the text is actually changing or not, making sure your buy weapon is targeting the proper textmeshpro object.
     

    Attached Files:

  9. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    I have checked and the same GameObject is attached to both objects. A side note here to consider. I even duplicated the original object and it still showed the same issue.
     
  10. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    I'll still send a new video with a minimized game view.
     
  11. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    Here's the new video link

     
  12. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    I didn't show in the video I can still buy its respective gun that I'm near.
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    What is often happening in these cases is one (or more) of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  14. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    The code would have to be executing perfectly normal because it works for the first instance of the prefab. I'll try your other methods to see if they work.
     
  15. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    I would not see any need for a debug statement because I have the inspector and debug mode in the inspector to show me what is happening.
     
  16. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    I might have to re write my code to hopefully fix the issue but that is my last case resort.
     
  17. Adastour

    Adastour

    Joined:
    Jan 6, 2021
    Posts:
    48
    This fixes the issue but is certainly not ideal. If I duplicate the original parent to be its separate object and do that for as many wall weapons there are, and then assign them to different wall weapons it works but again its not ideal.