Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MainframePT · Mar 17, 2014 at 05:40 PM · gameobjectarrayliststore

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vexe · Mar 17, 2014 at 08:56 PM

Well, my first answer was "Yes"

For a couple of reasons:

  1. 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.

  2. 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...

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MainframePT · Mar 18, 2014 at 09:45 AM 0
Share

$$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!

avatar image vexe · Mar 18, 2014 at 09:51 AM 0
Share

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.

avatar image MainframePT · Mar 18, 2014 at 03:45 PM 0
Share

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.

avatar image vexe · Mar 18, 2014 at 03:47 PM 0
Share

If you think my answer helped, please consider ticking it. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges