Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Regex within scripts

Discussion in 'Scripting' started by eternalnoob, Feb 11, 2010.

  1. eternalnoob

    eternalnoob

    Joined:
    Jan 24, 2010
    Posts:
    53
    Hi All,
    How to use regular expression style constructs on strings within scripts? For example, I have a whole lot of GameObjects called "pawn1a", "pawn1b", "pawn2a" etc. How can I do this:

    Code (csharp):
    1. GameObject.Find("pawn"[0-9,a-z]*);
    Any help much appreciated.

    EN
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should use a tag for your pawn objects, and use FindGameObjectsWithTag.

    --Eric
     
  3. eternalnoob

    eternalnoob

    Joined:
    Jan 24, 2010
    Posts:
    53
    Thanks Eric, I'm getting a "tag is not defined" exception, and I see in the script reference that "Tags must be declared in the tag manager before using them." I'm generating the game objects dynamically then dynamically assigning a tag, so how can I declare them in the tag manager before I know what they are?

    Cheers,
    EN
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make a tag called "pawns", then assign that to your game objects when they are made.

    --Eric
     
  5. eternalnoob

    eternalnoob

    Joined:
    Jan 24, 2010
    Posts:
    53
    Hi Eric, I did that and it works out fine, thanks very much for the advice. But I still wonder how I can use regex style constructs to search through all tags and find, for example, any tags that start with "pawn" followed by a number and letter. If I have a collection of tags, "pawn1a", "pawn1b"... "pawn2a", "pawn2b"... and so on, how do I do something like this:
    Code (csharp):
    1. FindGameObjectsWithTag("pawn[0-9,a-z]*");
    2.  
    Looking at the string object in the reference, it doesn't seem to contain functionality like IndexOf etc that might be useful to achieve the above, so I'm thinking regular expressions are the way to go?
    As always, your help is much appreciated.
    EN
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There is no functionality like that. The whole point of using a tag is that you do not use such tags as "pawn1b", "pawn2a", etc. You use a single tag called "pawns" in order to find all game objects called "pawn1b", "pawn2a", etc.

    --Eric
     
  7. eternalnoob

    eternalnoob

    Joined:
    Jan 24, 2010
    Posts:
    53
    Ah, okay, now I get it, that makes sense. However, now I'm getting this error: "Static member `UnityEngine.GameObject.FindGameObjectsWithTag(string)' cannot be accessed with an instance reference, qualify it with a type name instead" which I really don't understand. I'm writing this in C#, based on the example in the FindGameObjectsWithTag page in the scripting reference.

    Cheers,
    EN