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 /
This question was closed Dec 15, 2014 at 04:29 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Essential · Dec 14, 2014 at 06:47 PM · classdatagenerics

Trying to learn Generics

I've stored data in the inspectors of various Prefab objects (e.g., LevelData, EnemyData, etc.)

As these all need to be instantiated and then kept around for referencing, I'm trying to group them all under a persistent DataManager object that will instantiate them all on awake.

For this, I'm trying to create a class that can store the different data scripts generically, but I haven't really touched generics before. I've written the below to demonstrate, but I know it's wrong… can anyone help?

 using UnityEngine;
 using System.Collections;
 
 public class DataManager : MonoBehaviour {
 
     public class DataObjects where T : Component
     {
         public Object dataPrefab;
         private T instance;
     }
 
     public DataObjects[] dataObjects;
     static DataManager instance;
 
 
     void Start ()
     {
         DataManager dm = FindObjectOfType( typeof(DataManager) );
         if ( dm != null && dm != this )
             Destroy( gameObject );
 
         instance = this;
         DontDestroyOnLoad( gameObject );
         InstantiateData();
     }
 
     private void InstantiateData ()
     {
         for ( int i=0; i < dataObjects.Length; i++ )
         {
             GameObject tmpObj = Instantiate( dataObjects[i].dataPrefab );
             tmpObj.transform.parent = transform;
             dataObjects[i].instance = tmpObj.GetComponent< T >();
         }
     }
 }
 
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

  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Dec 14, 2014 at 07:25 PM

Your question is not really suitable here since it is basically a question on how to use generic and you will get long and detailed answers online, particularly from msdn.

Your error is there:

  public class DataObjects<T> where T : Component
  {
      public Object dataPrefab;
      private T instance;
  }

you are missing the diamond indicating the generic. And you use:

  DataObjects <ScriptName> object = new DataObjects<ScriptName>();

Now your instance is of type ScriptName but does not have any object it will point to and no way to assign any since it is private. You should have a property or method to set it:

  public class DataObjects<T> where T : Component
  {
      public Object dataPrefab;
      private T instance;
      public DataObject<T>(){}
      public DataObject<T>(T reference)
      {
            instance = reference
      }
      public DataObjects<T>Instance{get{return instance;}}
  }


 void Start()
 {
     DataObjects<ScriptName>data = new DataObjects<ScriptName>(gameObject.AddComponent<ScriptName>());
 }

In this case, the instance will point to a component attached to the game object since they have to.

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 Essential · Dec 15, 2014 at 04:28 PM 0
Share

Thanks. I tried reading the $$anonymous$$SDN docs but was still struggling to understand. Your code has helped!

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Reading and Storing External Data into Memory (From Text) 1 Answer

Database-like arrays 1 Answer

How to save and load any data type? 1 Answer

Lists and Structs instead of Arrays? 4 Answers

Storage of types in Structs and Classes 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