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:
13937-capture2.jpg

However I would like to achieve something like this:
13938-capture.jpg

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.

1 Answer

1

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.

Thanks I'll figure it out!

–

Have you solved your issue yet?

–