[My fault, do not read. Please delete this!]

EDIT: I’m a moron… I had disabled info notifications in the Console, so I didn’t see the code working. If you’re a moderator, please delete this embarrassment.

Hello!

So since Unity doesn’t support multiple tags on the same GameObject, I wanted to make my own system. It was very simple: have a script with a dictionary on an object, and have a public function to call to check if it has the tag. Here’s the code:

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


public class MyTagList : MonoBehaviour {

    //point of this script is to have multiple tags
    //Not ideal, however I'm kinda short on time and lazy so I'll use this for now
    public Dictionary<string, string> tags = new Dictionary<string, string>();
    public string IsClickable; //must be "true" or "false"
    public string Tag1Value;
    public string Tag2Value;
    //public float OriginalHP;
    //public float CurrentHP;
    public string SearchResult;
    //public ArrayList ListOfTags;
    // Use this for initialization
    void Start ()
    {
        tags["IsClickable"] = "testing";
        FindTagName("IsClickable");
    }

    public void FindTagName(string TagToFind)
    {
        foreach (KeyValuePair<string, string> KVP in tags)
        {
            Debug.Log("Found key " + KVP.Key + " with value " + KVP.Value);
        }

        if (tags.ContainsKey(TagToFind))
        {
            SearchResult = TagToFind;
        }
        else
        {
            SearchResult = TagToFind + " was not found in dictionary.";
        }
    }
}

It used to work in one of my old projects. However, when I run the code, SearchResult is “IsClickable” (as expected", but the foreach function doesn’t show anything in the console at all. My key (IsClickable) has a value of “testing”, so it is a key value pair. I’ve used dictionaries and KeyValuePair before, so I’m very confused as to what isn’t working here.

Any idea why I can use tags.ContainsKey but can’t find KeyValuePairs in this script?

Thanks for any ideas.

EDIT: I’m a moron… I had disabled info notifications in the Console, so I didn’t see the code working. If you’re a moderator, please delete this embarrassment.

Looks like it should work. Could it be something silly like you have Info-level messages hidden in your Console?

(Not that I’ve ever done that… no, certainly not me! Oh my, look at the time…)

1 Like

Yes, you read that seconds before I edited my post. This is what I get for writing code at almost midnight.

1 Like

Not actually the worst thing I’ve done…

Sometimes I spend ages debugging a line in Unity to realise I hadn’t hit save in VS…

Occasionally I’ll set up a complex chain of logic to fire on a button press. Nothing works because I’ve forgotten to hook up the method to the button…

Or I’ll build a stub method with ‘return 0;’. Later I come back and program in the rest of the method. But I forget to change ‘return 0;’ to ‘return result;’…

I’m sure we could make a whole thread documenting our most embarrasing face palm moments in debugging.

3 Likes