Search Unity

How to scan clones into a list or array

Discussion in 'Getting Started' started by InsensitveJ0ker, Aug 13, 2019.

  1. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Hi. I have a project that uses basically a board. So basically one game object creates a whole bunch of clones of another object. Then a 3rd object uses the GameObject.FindGameObjectsWithTag function to scan all the clones into an array.

    upload_2019-8-13_10-48-16.png

    upload_2019-8-13_10-48-36.png
    And it gives me this error.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I suggest you study line 11 of ScanBoard.cs. If you still can't figure it out, post that script (at least up to line 11) here.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm going to guess you are confusing object names with tags on ScanBoard.cs line 11.
     
    InsensitveJ0ker and JoeStrout like this.
  4. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Here's the code.

    GameObject[] board;
    // Start is called before the first frame update
    void Start()
    {
    board = GameObject.FindGameObjectsWithTag("Blank Space(Clone)"); //This is line eleven
    print(board.Length);
    }
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then @Joe-Censored's guess was correct. You have supplied an object name where it wants a tag.
     
    InsensitveJ0ker likes this.
  6. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    What do you mean?
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Found via Google search for "unity tag" https://docs.unity3d.com/ScriptReference/GameObject-tag.html By the way, all your objects have the same name, so which object did you expect to find via "Blank Space(Clone)", if you were using the name? It would likely just find the first one. So the solution looks to add a tag when you clone the object, and then use it here.
     
    InsensitveJ0ker likes this.
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    All of these are the names of the objects.

    upload_2019-8-13_10-48-16.png

    This line of code is trying to create a list of objects by their tag.
    Code (csharp):
    1. board = GameObject.FindGameObjectsWithTag("Blank Space(Clone)");
    Names and tags are not the same thing.

    https://docs.unity3d.com/Manual/Tags.html

    Unfortunately the "best" solution would be to use a FindGameObjectsWithName but that doesn't exist for some oddball reason so we're going to use Linq to do it for us.

    Add this to the top of the file.
    Code (csharp):
    1. using System.Linq;
    Then replace that line with this.
    Code (csharp):
    1. board = Resources.FindObjectsOfAllType<GameObject>().Where(obj => obj.name == "Blank Space(Clone)").ToArray();
    One last tidbit to understand is that everyone of the "Find" functions has horrible performance compared to just about every alternative implementation. If you want to have a list of objects the best way to make it is as you're creating them. The code that creates an object returns a reference to it. Simply add that to a list and have other scripts access that list.
     
    Last edited: Aug 15, 2019
    InsensitveJ0ker and Joe-Censored like this.
  9. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    Wow, that really worked. By the way, what does system.linq access? Or like what is it used for?