- Home /
Using class arrays for multiple Variables per element also Naming Elements
I want to populate an array so that each element has 2 fields in the inspector, a string and a bool. I have 5 objects with the tag "TouchZone" in the scene. I want to populate the array using GameObject.FindGameObjectsWithTag("TouchZone") I have the following:
[System.Serializable]
public class Data
{
public string objectName;
public bool objectBool;
}
public Data[] dataArray;
I know I need a for loop, I just don't understand how to add to the array or how to write the for loop properly to add the data to the array properly.
int x = GameObject.FindGameObjectsWithTag("TouchZone").Length;
for (int i = 0; i < x; i++)//Get the list length of Scenes
{
//What do I add here?
}
I figure I need to add it to the for loop above but I dont know the proper code to add the data into the case array
Thanks!
Answer by Bunny83 · Feb 11, 2021 at 04:40 AM
You should rethink your approach. First of all an array can not change its size. The size of an array need to be specified when the array is created. Second it wasn't really clear what exact data you want to store in that string and boolean value. If it's the name of the objects you found through the FindGameObjectsWithTag method it would make more sense to store the actual references to those objects in your class. Specifically something like that:
[System.Serializable]
public class Data
{
public GameObject obj;
public bool objectBool;
}
public Data[] dataArray;
// [ ... ]
var objs = GameObject.FindGameObjectsWithTag("TouchZone");
// create new Data array with the same number of elements as we have in the objs array.
dataArray = new Data[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
// create new Data object
var tmp = new Data();
// set the values you want.
tmp.obj = objs[i];
tmp.objectBool = false;
// store the Data object in our dataArray
dataArray[i] = tmp;
}
If, for some reason you really want to store a copy of the names of the objects, instead of storing the reference to the object in the class you just store the name (or whatever you want from those object). So if the Data object looks like your original one, you would do something like
tmp.objectName = objs[i].Name;
Answer by Panundrum · Feb 11, 2021 at 05:03 AM
OH MY god you are my HERO! I have been pounding my head on the desk for HOURs and hours! It finally displays the info I want:
My full code for just this example that is working thanks to @Bunny83 is this (for those that were interested):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class boolArray : MonoBehaviour
{
//public bool[] touchZonesToggle;
[System.Serializable]
public class Data
{
public GameObject obj;
public bool objectBool;
}
public Data[] dataArray;
void Start()
{
// [ ... ]
var objs = GameObject.FindGameObjectsWithTag("TouchZone");
// create new Data array with the same number of elements as we have in the objs array.
dataArray = new Data[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
// create new Data object
var tmp = new Data();
// set the values you want.
tmp.obj = objs[i];
tmp.objectBool = false;
// store the Data object in our dataArray
dataArray[i] = tmp;
}
}
void Update()
{
}
}
I added the public string name in the class and the name in the for loop so that it would set the fields in the elements to the name of the object. (Keep in $$anonymous$$d I also changed the var to the proper variable type for my own knowledge) I wanted to change the name of the elements too when I first started this, so I am happy to add it here now.
//Class for the layout (Blueprint) of a later array
[System.Serializable]
public class Data
{
//the name string is to set the Element name in the inspector
//Hide the name [Field] in the inspector because it shows the name in the element
[HideInInspector]
public string name;
public GameObject obj;
public bool objectBool;
}
//The Public array for the Class:
public Data[] dataArray;
void Start()
{
// Do a search for gameobjects with the matching tag store that in a var
GameObject[] objs = GameObject.FindGameObjectsWithTag("TouchZone");
// create new Data array with the same number of elements as we have in the objs array.
dataArray = new Data[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
// create new Temporary Data object
Data tmp = new Data();
// set the values you want.
tmp.obj = objs[i];
tmp.objectBool = false;
//This sets the name of the element
tmp.name = objs[i].name;
// store the Data object in our dataArray
dataArray[i] = tmp;
}
}
Hope this helps someone else too. This is the outcome:
Your answer
Follow this Question
Related Questions
Setting arrays equal to each other issue 2 Answers
Using variables inside a GameObject[] array 1 Answer
Multidimensional arrays - editing one element within one of the arrays 1 Answer
Multidimensional array trouble 2 Answers
Prefabs instantiated from an array are keeping their public int value 1 Answer