- Home /
Using variables inside a GameObject[] array
Hello fellow programmers,
I'm trying to use a class inside my GameObject[] array so I can access them in the inspector however, I'm not entirely sure how to do that, right now what I got is something like the following:
However I would like to achieve something like this:
This is the base of my code right now:
[System.Serializable]
public class tiles
{
public GameObject[] Plants;
public GameObject[] Trees;
public GameObject[] Rocks;
}
[System.Serializable]
public class tileFunctions
{
public bool isInteractive; //can u click on it and do things with it?
public bool isUsed; //is it on cooldown after use?
public int maxReceivings; //if gathered how many items max
}
I would like to use the tileFunctions inside Plants, Trees and Rocks. So that I could use them as follows for example: groundTiles[0].Plants[0].isUsed
Thanks! Regards, Shady.
Answer by jmonasterio · Jan 03, 2016 at 04:04 AM
Assuming you have Plant, Tree, and Rock classes (that each inherit from monoBehaviour), you might get more mileage from keeping arrays of the specific types instead of gameObjects.
[System.Serializable]
public class tiles
{
public Plant[] Plants;
public Tree[] Trees;
public Rock[] Rocks;
}
Then in your Plant object, for example, you can have a tileFunctions object like:
public class Plant : MonoBehaviour
{
public tileFunctions tile_funcs;
}
and you can access this in code as:
tiles.Plants[2].tile_funcs.isUsed = true; //Example
Although that may give you what you're asking for, it's not really the unity way AFAIK. Really, your TileFunctions should be a MonoBehaviour. And then you add your TileFunction component/script to your Plant.
Your answer
Follow this Question
Related Questions
Using class arrays for multiple Variables per element also Naming Elements 2 Answers
Dynamic Array Generation via Inspector 1 Answer
how subtract total value element of array ?? 1 Answer
How to see what element is the current one 2 Answers
How do I access a variables created in a function with another function? 1 Answer