Search Unity

Question Can I use FindWithTag() the way I'd like to use it?

Discussion in 'Scripting' started by rosco_y, Feb 12, 2024.

  1. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    99
    I construct some class fields:

    Code (CSharp):
    1.  
    2. const string CENTERTAG = "CENTER";
    3. Dictionary<int, List<SudoCube>> _front2backLayers;
    4. Dictionary<int, List<SudoCube>> _top2bottomLayers;
    5. Dictionary<int, List<SudoCube>> _left2rightLayers;
    which I then wrap in a "cDictionary" class I've created:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. public class cLayerDictionary
    8. {
    9.     Dictionary<int, List<SudoCube>> _layers;
    10.     bool _reversed = false;
    11.  
    12.     string _name;
    13.     public bool[] LayerIsActive { get; set; }
    14.  
    15.  
    16.  
    17.     public cLayerDictionary(Dictionary<int, List<SudoCube>> dict, string name = "UNK")
    18.     {
    19.         LayerIsActive = new bool[g.PSIZE];
    20.         for (int i = 0; i < g.PSIZE; i++)
    21.             LayerIsActive[i] = true;
    22.         _layers = dict;
    23.         _name = name;
    24.     }
    25.  
    26.     public Dictionary<int, List<SudoCube>> Layers
    27.     {
    28.         get { return _layers; }
    29.     }
    30.  
    31.  
    32.     /// <summary>
    33.     /// Reverses The Layers in this dictionary.
    34.     /// </summary>
    35.     public void ReverseDictionary()
    36.     {
    37.         //LayerIndices = LayerIndices.Reverse().ToArray();
    38.         _layers = _layers.Reverse().ToDictionary(x => x.Key, y => y.Value);
    39.  
    40.         _reversed = !_reversed;
    41.  
    42.         if (g.CurLayer > 0)
    43.             g.CurLayer = 0;
    44.         else
    45.             g.CurLayer = 8;
    46.     }
    47.  
    48.     public string Name
    49.     {
    50.         get { return _name; }
    51.         set { _name = value; }
    52.     }
    53.  
    54.  
    55.     public bool Reversed
    56.     {
    57.         get { return _reversed; }
    58.  
    59.         set { _reversed = value; }
    60.     }
    61.  
    62. }
    63.  
    and then construct them in this manner:

    Code (CSharp):
    1.     private void constructLeftToRightLayers()
    2.     {
    3.         List<SudoCube> layerList = new List<SudoCube>();
    4.         for (int col = 0; col < g.PSIZE; col++)
    5.         {
    6.             for (int layer = 0; layer < g.PSIZE; layer++)
    7.             {
    8.                 for (int row = 0; row < g.PSIZE; row++)
    9.                 {
    10.                     layerList.Add(g.Data[layer, row, col]);
    11.                     if (row == 4 && col == 4)
    12.                         g.Data[layer, row, col].gameObject.tag = CENTERTAG;
    13.                 }
    14.             }
    15.             _left2rightLayers.Add(col, layerList);
    16.             layerList = new List<SudoCube>();
    17.         }
    18.     }
    19.  
    I hoped that I could then use a function to locate these objects with a CENTERTAG
    using this kind of property:
    Code (CSharp):
    1.     public SudoCube CurrentCenterCube
    2.     {
    3.         get
    4.         {   // int aLayer = n
    5.             return _usingLayerDictionary.Layers[aLayer].FindWithTag(CENTERTAG);
    6.         }
    7.     }
    8.  
    Of course, this can't even be compiled.

    Can anyone tell me what I can do to find my "SudoCubes" with the CENTERTAG tags?
    or suggest another way to do this?

    Thank you,

    Rosco-y
     
    Last edited: Feb 12, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    GameObject.FindWithTag
    is a static method. You can't call it from an instance of that type, as it belongs to the type, not any given instance.

    If you know the size of whatever this data structure is, then you can probably just use math to work that out.
     
    rosco_y likes this.
  3. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    99
    :) Thank you!
    I'm thinking of returning the List as an array, converting that to 2d (should be 81 elements), and then taking array[4,4] as the center object.

    Rosco-y
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    That sounds like overkill when you could divide the number of elements in the collection by 2.
     
    rosco_y likes this.
  5. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    99
    You are right again. I tried 2dArray[4,4] and array[40], they both give the same result.

    Thank you,

    Rosco-y