Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JuanForero · Sep 27, 2019 at 09:50 PM · listclassproceduraldynamicpointer

I neet to make like a Hyper Procedural Dynamic List on Unity

I need a super dynamic data struct, a list that saves any type of structure or a class. Or a struct that saves any kind of data;

how to make this work ?

  list <class1> list_class_one; list <class2> list_class_two; list<> Dynamic_Struct
  list_class_two(2).a = list_class_one(4)-> b; //add item to list form other class and conect
 
  Dynamic_Struct.add(list_class_one);  // or a struct that save any type of class
  Dynamic_Struct.add(list_class_two);
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mouton · Sep 28, 2019 at 10:34 PM

Use a generic List using an Interface.

 public interface ISuperClass
 {
 }
 
 public class SuperClass : ISuperClass
 {
 }
 
 public class ChildClassA : SuperClass
 {
 }
 
 public class ChildClassB : SuperClass
 {
 }
 
 public class AnotherClass : MonoBehaviour
 {
     public List<ISuperClass> globalList = new List<ISuperClass>();
 
     private void Start()
     {
         globalList.Add(new ChildClassA());
         globalList.Add(new ChildClassB());
         globalList.Add(new SuperClass());
         // ..;
     }
 }


Although, you won't be able to add any kind of instance in this List because existing class inheritance cannot be changed.

An interface is not required to do this, so you could make a List of Object, which is the base class of many other C#/Unity classes

 List<Object> globalList;


Edit

To access the items of these lists, you need to typecast the elements to your needs.

 public class AiBehaviour : Object
 {
     public void Disable()
     {
         // ...
     }
 }
 
 public class EnvironmentBehaviour : Object
 {
     public void Trigger()
     {
         // ...
     }
 }
 
 public class Example : MonoBehaviour
 {
     private void Start()
     {
         List<Object> globalList = new List<Object>();
 
         globalList.Add(new AiBehaviour());
         globalList.Add(new EnvironmentBehaviour());
 
         foreach (Object el in globalList)
         {
             // `el` is an Object, we need to convert it to the correct type before calling a method,
             // `Object` class provides a `GetType` method to retrieve in-memory type
             switch (el.GetType())
             {
                 case AiBehaviour:
                     ((AiBehaviour)el).Disable();
                     break;
                 case EnvironmentBehaviour:
                     ((EnvironmentBehaviour)el).Trigger();
                     break;
                 default:
                     Debug.Log("No valid type cast for this object");
                     break;
             }
         }
     }
 }


As you can see, this result in an undesirable degree of complexity, and should be avoided. To reduce the complexity, you could create an interface class with methods to do the typecasting and use a messaging system to make generic calls to the children, then let the children either valid or discard the message, but this will just move the complexity to the children and make the calling code not understandable:


 _privateGlobalList.SendMessage(MessageType.Disable, typeof(AiBehaviour));


In conclusion: just don't try to make a list of everything, with a basic setup you will get better readability/performances with the FindObjectsOfType method.

Comment
Add comment · 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
0

Answer by JuanForero · Sep 29, 2019 at 04:13 AM

Hi @Mouton thanks for your answer, I need a very dynamic data struct, but for now I think this is the thing that I looking for.

  List<Object> globalList;
  globalList.Add(node);

but this general list type Object, how you can acces to a list element data

 gloabalList[i].node.current_pos;  // dont work when you access like a normal list, list<normal_class> normal_list
Comment
Add comment · Show 1 · 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 Mouton · Sep 29, 2019 at 10:33 AM 0
Share

Hi, I have updated the answer with details on how to access list elements. If it answers your issue, please accept the answer. If it doesn't please add details with comments on my answer and not by creating an answer. Thank you ! :)

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

123 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 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 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 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 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 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

A node in a childnode? 1 Answer

Generic List 'Contains' always returns false 1 Answer

Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers

How do I save an array of all different variables? 0 Answers

Collisions and Lists 1 Answer


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