I'm not a coder, im an artist, so this stuff doesn't make a ton of sense to me. I know enough to know I don't know enough, and i'm old enough to know my brain doesn't 'get' code... that said for context of what level of help I need :) ...

I'm trying to get an AI path based on an object tag. When I instantiate the object this script is on, it should look for 'mytag' on another object in the scene, and set that as 'target'

    public class pickAItarget : VersionedMonoBehaviour
        /// <summary>The object that the AI should move to</summary>
        private Transform target;
        public string mytag;
        IAstarAI ai;

        void Start()
            GetComponent<pickAItarget>().target = GameObject.FindWithTag("mytag").transform;

The script compiles fine and im able to put in the tag I want to find in the inspector but when the object instantiates I get

UnityEngine.GameObject.FindWithTag (System.String tag) (at C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:195)
Pathfinding.pickAItarget.Start () (at Assets/Scripts/pickAItarget.cs:26)

I feel like I HAVE defined mytag, but obviously I don't understand something.

  • 1
    \$\begingroup\$ What if you remove the double quotes around mytag in your function? \$\endgroup\$ – Vaillancourt Mar 9 '20 at 1:52
  • \$\begingroup\$ that... would be the correct answer. i feel both stupid, and happy :) thank you. \$\endgroup\$ – tenabraex Mar 9 '20 at 1:55
  • \$\begingroup\$ No worries :) someone will write a decent answer for this \$\endgroup\$ – Vaillancourt Mar 9 '20 at 1:56
  • You don't need to use "GetComponent()" from pickAItarget, as you don't need to refer yourself from yourself.
  • If there are multiple tags with the same name in your hierarchy then it will find the first object only. So if there are multiple objects with same tag then you should try "GameObject.FindGameObjectsWithTag", this will return array of all objects with same tag i.e. public static GameObject[] FindGameObjectsWithTag(string tag);

If this Ans is not what you are looking for, please tell us

  • Is this script is added on the gameobject that you are instantiating ?
  • Is the instantiated object contains the "mytag" tag?
  • \$\begingroup\$ Did you read the comments on the question? \$\endgroup\$ – Vaillancourt Mar 9 '20 at 10:51
  • \$\begingroup\$ Hello! Your answer is decent and useful. I suggest you undelete it and add the missing elements that was added in the comments. Sorry if my previous comment to your question seemed a bit harsh; I was only trying to highlight that the core issue was not addressed in your answer. \$\endgroup\$ – Vaillancourt Mar 9 '20 at 14:47

When you put some text in double quotation marks, like:


What you're saying to the compiler is: "this is literally just text. Please just treat it as a string of characters, and don't try to parse it as code instructions or the name of a variable".

So instead of quitting the game, the example above just gives us a string containing literally those characters - what we call a string literal.

By that logic,


This code says 'Find a game object with the tag "mytag" ' - not the contents of the variable named mytag, but look for a game object whose tag is literally m-y-t-a-g.

If you want to refer to the variable named mytag, which might contain some other text entirely, like "Player", then just delete the double quotation marks around it and leave it plain, like the other code instructions.


This says "Find a game object whose tag matches the text stored in the variable mytag"


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.