- Home /
Storing complex GameObject structure
Greetings,
I have a project with the following GameObject structure:
ProjectionSurface1
DestinationPoints
DS1
DS3
DS3
DS4
Plane
SourcePoints
SS1
SS2
SS3
SS4
There will be multiple "ProjectionSurfaceX", where X denotes the number of the surface. I will need to access information contained in all of these GameObjects, my issue right now is how should I store all these GameObjects so that they can be easily accessed?
Answer by vexe · Mar 17, 2014 at 08:56 PM
Well, my first answer was "Yes"
For a couple of reasons:
I read your question incorrectly: "Strong complex GameObject structure" and totally missed the "how" in "how should I store" and so now it's a yes/no question.
I was actually having a very bad day, and I wanted a relief in the form of a laugh.
For the answer:
It all depends on your needs. It would have been more helpful if you provided more details as to who's accessing the points, what part of the game object hierarchy you're interested in, why have a "DestinationPoints" instead of the points just being children to the ProjectionSurface? etc.
Here's one way to store them (based on the current hierarchy you posted):
public class ProjectionSurface : MonoBehaviour
{
public List<GameObject> destPoints = new List<GameObject>();
private void Awake()
{
PopulatePoints();
}
private void PopulatePoints()
{
destPoints.Clear();
foreach(Transform point in transform.GetChild(0))
destPoints.Add(point.gameObject);
}
}
But this is not safe, it's depending on the fact that "DestinationPoints" is the first child - so you might be better of writing an extension method for Transform
that gets a child by name, like:
public static class TransformExtensions
{
public static Transform GetChild(this Transform inside, string wanted, bool recursive = false)
{
foreach (Transform child in inside) {
if (child.name == wanted) return child;
if (recursive) {
var within = GetChild(child, wanted, true);
if (within) return within;
}
}
return null;
}
}
And so now the Populate
method becomes:
private void PopulatePoints()
{
destPoints.Clear();
foreach(Transform point in transform.GetChild("DestinationPoints"))
destPoints.Add(point.gameObject);
}
But now you might have trouble if you rename the DestinationPoints game object...
Safest solution (what I would go for) would be to store a reference to the DestinationPoints GO instead of having to look for it:
public class ProjectionSurface : MonoBehaviour
{
public List<GameObject> destPoints = new List<GameObject>();
public GameObject destinationPointsGO; // set via inspector
private void Awake()
{
PopulatePoints();
}
private void PopulatePoints()
{
destPoints.Clear();
foreach(Transform point in destinationPointsGO.transform)
destPoints.Add(point.gameObject);
}
}
Now you can easily access your points via myProjSurface.destPoints[index]...
Did that help? if not just ask...
$$anonymous$$y thinking here was around the concept of "struct" of the C language, store each ProjectionSurfaceX as a struct allowing easy access to its properties, plus being able to easily add SurfaceProjection without having to write additional code to support it. Thanks for your reply, I'll give it a try!
why would you think of structs? what properties do ProjectionSurface have? - I thought you asked for a way to easily access your points?
Btw same idea could be followed to access all the ProjectionSurfaces. i.e. you might have a hierarchy like:
Base
... ProjectionSurface1
... ProjectionSurface2
... ProjectionSurfaceN
So you just stick a similar script to Base
now...
public class StuffContainer : $$anonymous$$onoBehaviour
{
public List<GameObject> stuff = new List<GameObject>();
private void Awake()
{
PopulatePoints();
}
private void PopulatePoints()
{
stuff.Clear();
foreach(Transform t in transform)
stuff.Add(t.gameObject);
}
}
Please let me know if I miss-understood something.
Ended up doing something very similar to the approach you suggested, only had some issues when it comes to add game objects to lists, the order that Unity used (seems the default order is based on object creation) wasn't the one I needed but managed to solve it. Thanks again for your help.
If you think my answer helped, please consider ticking it. Thanks.
Your answer
Follow this Question
Related Questions
Enabled all gameobjects in array 1 Answer
Deactivate current object in array, activate next one? 2 Answers
How to put gameObjects to the list? 4 Answers
2D GameObject Array to 2D List 1 Answer
List all children in array? 5 Answers