You can't use a class as a pointer. Maybe this has changed in the latest C# but I am not sure. That simply will not work. Any reference type will not work with pointers.
Simple type such as int
,float
and bool
and others can be used as pointers.
Even if you declare the GameObject go
variable in the Tile
struct, it is not fine.
It is not recommended to declare a reference type inside a struct especially in a gaming app because that will make the garbage collector unnecessarily scan for objects to destroy in the struct which slows stuff down. This is even worse when you have millions of those structs with reference objects inside them.
My suggestion is to use integer instead of GameObject *. That integer should be initialized with the GameObject's instanceID
instead of the the GameObject reference or pointer.
You can then use Dictionary
to map the instanceID
(int) to the GameObject
. When you want to access the GameObject, provide the intanceID
stored in the Tile
struct. This will speed up everything.
Example:
struct Tile
{
int goID;
public Tile(int goID)
{
this.goID = goID;
}
}
Usage:
Dictionary<int, GameObject> idToObj = new Dictionary<int, GameObject>();
Tile[] tiles = new Tile[1000];
GameObject obj = new GameObject("obj");
//Create new Instance with Instance ID
tiles[0] = new Tile(obj.GetInstanceID());
//Add to Dictionary
idToObj.Add(obj.GetInstanceID(), obj);
Finally, it worth testing this to see which one is faster yourself. Test with GameObject declared in the struct then with the method above that uses int
and Dictionary
to work.