Not Finding TMP_Text File Attached to Game Object

I’m having problems with finding the TMP_Text file attached to a Game Object. My game generates a counter to display the order that a list of cards will be played in and I have a function to remove those cards from the order. When I do this, I want to run a loop as part of that function that checks each available card to see if it is:

  1. already a card in the current order
  2. a value greater than the card that was removed (ex: if card#2 is removed then #3 becomes the new #2 slot, etc.)

My code is as follows:

for (int i = 0; i < 3; i++)
{
    if (cardSlots[i].isSelected == true)
    {
        tempVar = i;
        tempVar++;
        tempStr = tempVar.ToString();
        currCntr = GameObject.FindGameObjectWithTag("clone" + tempStr);
        tempText.text = currCntr.GetComponent<TMPro.TextMeshProUGUI>().text;

When it gets to the line of code to GetComponent, it produces a NullReference saying that the reference is not set to an instance of the object. Can anyone help me with figure out why its not seeing the TMP_Text file attached to the Game Object in question?

Thanks in advance!

Just make a properly-typed field and drag it in. If it needs to be dynamic, make an “example” object and clone it X number of times, which is how I would make any kind of a card game. See the attached example code for how to make a dynamically arbitrary number of UI objects with different parts (sprite, text, action, etc.)

If you insist on doing it your way above… then it is time to start debugging.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.

The longer your lines of code are, the harder they will be for you to understand them.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

Keep in mind that using GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

Here’s the bare minimum of stuff you absolutely MUST keep track of if you insist on using these crazy Ninja methods:

  • what you’re looking for:
    → one particular thing?
    → many things?
  • where it might be located (what GameObject?)
  • where the Get/Find command will look:
    → on one GameObject? Which one? Do you have a reference to it?
    → on every GameObject?
    → on a subset of GameObjects?
  • what criteria must be met for something to be found (enabled, named, etc.)

If you are missing knowledge about even ONE of the things above, your call is likely to FAIL.

This sort of coding is to be avoided at all costs unless you know exactly what you are doing.

Botched attempts at using Get- and Find- are responsible for more crashes than useful code, IMNSHO.

If you run into an issue with any of these calls, start with the documentation to understand why.

There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.

In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.

It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.

9557998–1351300–DynamicUIDemo.unitypackage (94 KB)