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);
}
}
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.
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.
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.
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:
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.
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.