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 Gothic_Anatomist · Oct 05, 2016 at 06:21 PM · arraysclassesinitialisation

Initialisation of array across classes

Hey folks, I've created a few different classes to get details of a book:

 [System.Serializable]
 public class Textbook
 {
     public Chapter[] book;
 
     public Textbook(int NumOfChapters)
     {
         book = new Chapter[NumOfChapters];
         
     }
 }
 
 [System.Serializable]
 public class Chapter
 {
     public string chapter;
     public SubChapter[] subChapter;
 
     public void SetNumber (int NumOfSubchapters)
     {
         subchapter = new SubChapter[NumOfSubchapters];
     }
 }
 
 [System.Serializable]
 public class SubChapter
 {
     public string subchapter;
     public string imageVideo;
     public string textFile;
 }

What I'm trying to do is initialise the subChapter array held within the Chapter class, with a specific number of sub chapters. So for example when initialising the textbook object, I use:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 public class BookManager : MonoBehaviour {
 
     public static BookManager ins;
     public Textbook chemistry1 = new Textbook(3);
 
     // Use this for initialization
     void Start () {
 
         ins = this;
     }
 }

and the constructor works fine. However, I'm scratching my head at how to then in turn set the number of the subchapter array for each chapter...if that makes sense?

Thanks in advance :)

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
1
Best Answer

Answer by bellicapax · Oct 05, 2016 at 08:02 PM

If you wanted to keep the initialization of your objects within the constructors and using the "new" keyword, you could simply add that information to your Textbook and Chapter constructors as an array of integers. The size of the array denotes how many chapters there are total and the item in the array denotes how many subchapters are in that chapter, so:

     [System.Serializable]
     public class Textbook
     {
         public Chapter[] book;
 
         public Textbook(int[] NumOfSubchapters)
         {
             book = new Chapter[NumOfSubchapters.Length];
             for (int i = 0; i < book.Length; i++)
             {
                 book[i] = new Chapter(NumOfSubchapters[i]);
             }
         }
     }
 
     [System.Serializable]
     public class Chapter
     {
         public string chapter;
         public SubChapter[] subChapter;
 
         public Chapter(int NumOfSubchapters)
         {
             subchapter = new SubChapter[NumOfSubchapters];
         }
     }

and then instantiate a book like so:

     public class BookManager : MonoBehaviour
     {
 
         public static BookManager ins;
         public Textbook chemistry1 = new Textbook(new int[] { 1, 3, 1, 5, 3, 5, 3 });
 
         // Use this for initialization
         void Start()
         {
 
             ins = this;
         }
     }

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 Gothic_Anatomist · Oct 07, 2016 at 11:23 AM 0
Share

Thank you, it worked perfectly! :)

avatar image
1

Answer by elenzil · Oct 05, 2016 at 07:53 PM

you could do something like this -

 public Textbook(int[] subChapterCounts)
 {
     book = new Chapter[subChapterCounts.Length];
 
     for (int n = 0; n < subChapterCounts.Length; ++n) {
         book[n] = new Chapter();    // thanks to bellicapax
         book[n].SetNumber(subChapterCounts[n]);
     }
 }
 

then you would initialize Textbook like:

 chemistry1 = new Textbook(new int[]{10, 2, 5});

that would get you a book w/ 3 chapters, and 10 sub-chapters in the first, 2 in the second, etc.

Comment
Add comment · Show 3 · 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 bellicapax · Oct 05, 2016 at 08:24 PM 1
Share

This line is going to throw a NullReferenceException because although you initialized the array, you have no instance of a Chapter at book[n].

 book[n].SetNumber(subChapterCounts[n]);
avatar image elenzil bellicapax · Oct 05, 2016 at 08:30 PM 0
Share

good point. will edit. our approaches are nearly identical.

avatar image Gothic_Anatomist · Oct 07, 2016 at 11:24 AM 0
Share

Thank you, this worked as well :)

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

6 People are following this question.

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

Related Questions

adding classes to arrays 2 Answers

Customize arrays and classes in inspector window 2 Answers

Can I preserve the values in a parent class when I overwrite it with a child class? 1 Answer

Inventory Help. 2 Answers

Classes : extend MonoBehaviour issues 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