• 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 mdolnik · Jan 03, 2014 at 08:33 PM · listserializationreferencearraylistgenerics

What is the best method to have a list of references to other scripts?

I am working in JS and having issues trying to find a way to provide some sort of reference list or class to organize / consolidate all scripts in a project.

Right now I have a way of accessing the "required" scripts in a singleton manager class, these are scripts I know the type of and know they will only be on one gameObject in the scene:

 class CManager extends MonoBehaviour {
     private static var Instance : CManager;
     public static function GetInstance() : CManager {
         if (Instance == null){ Instance =  FindObjectOfType(CManager); }
         if (Instance == null){ Debug.LogError("Can't Find CManager script on the Scene"); return null;
         }else{ return Instance; }
     }
     public class Reference {
         public var Reference1 : Reference1;
         public var Reference2 : Reference2;
         public var Reference3 : Reference3;
         public var RefObj1 : GameObject;
         public var RefObj2 : GameObject;        
     }
     public static function GetReference(refObject : System.Type) : Object{
         return Instance.reference.GetType().GetField(refObject.ToString()).GetValue(Instance.reference);
     }
     public static function GetReference(refObject : String) : Object{
         return Instance.reference.GetType().GetField(refObject.ToString()).GetValue(Instance.reference);
     }


And if I wanted to grab references from another script I would use something like:

 control = CManager.GetInstance();
 ref1 = control.GetReference(Reference1) as Reference1;
 ref2 = control.GetReference("RefObj2") as GameObject;

What I am stuck on is the optional references, what I am looking for is a way to have a reference class or list or something to be able to keep track of an unknown amount scripts of an unknown type. (Also need to have these available in the editor environment)

I have tried:

Class -> Issue is I don't know the amount of references or their type

ArrayList -> I am able to add to the list (with different types), but it cant be serializable (that I know of) so I end up losing the list

Generic List (List.< Object > ) -> Works, but same issue as ArrayList...

Are there any solutions or direction to point me in for this? or am I doing this all wrong?

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 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

Related Questions

2 problems with serialization of a generic list of a custom class 3 Answers

How to get smallest element and key from int list 1 Answer

A node in a childnode? 1 Answer

Serialize a List containing another List (List>) 4 Answers

How to save and load `List myList`? 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