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 MrsZorx · Jan 09, 2014 at 02:45 PM · c#editorstarteditorguiinitialization

Initialising List array for use in a custom Editor

I am having trouble initialising something before it gets used in a custom Editor.

I have a component attached to a game object, ResourceGenerator:

 public class ResourceGenerator : MonoBehaviour {
     
     //..some irrelevant public stuff here..
 
     public List<string>[] resourceSpawnTimes; // This is what i intend to modify in a custom editor
 
     void Awake() // I've also tried OnEnable
     {        
             // MatchInfo is another component in another game object
         MatchInfo info = (MatchInfo)GameObject.Find ("MatchInfo").GetComponent<MatchInfo>();
             // Array size defined by variables set in editor for the MatchInfo object
         resourceSpawnTimes = new List<string>[(int) (info.matchTimeSeconds / info.timeBetweenDropsSeconds)];
     }

     //..More irrelevant stuff..
 }


The List[] resourceSpawnTimes is what I want a custom editor interface for, so I've written ResourceSpawnTiming (It's not neccessary to read through it all but I wanted to include something reasonably complete just in case):

 [CustomEditor(typeof(ResourceGenerator))]
 public class ResourceSpawnTiming : Editor {
 
     MatchInfo info;
     int minutes;
     ResourceGenerator gen;
 
     ser List<string>[] resourceSpawnTimes;
 
     void OnEnable()
     {
         info = (MatchInfo)GameObject.Find("MatchInfo").GetComponent<MatchInfo>();
         gen = (ResourceGenerator)target;
     }
 
 
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector ();
         minutes = (int)(info.matchTimeSeconds / info.timeBetweenDropsSeconds);
         for(int i = 0; i < minutes; i++)
         {
             GUILayout.Label("Minutes into match: " + i + " - " + (i+1));
             // Display check boxes for all the wood resources
             foreach(string s in Resource.WoodResourceNames)
             {
 
                 bool currentlyEnabled = GetCurrentValue(i,s);
                 SetCurrentValue( i, s, currentlyEnabled,
                                 EditorGUILayout.Toggle(s, currentlyEnabled));
 
             }
 
         }
     }
 
     private bool GetCurrentValue(int minute, string resourceName)
     {
         // Check if resourceName is in the list for that minute
         foreach(string s in gen.resourceSpawnTimes[minute]) // This is where resourceSpawnTimes turns out to still be null
         {
             if(s == resourceName)
                 return true; // resource currently enabled
         }
         return false; // resource not 
     }

     private void SetCurrentValue(int minute, string resourceName, bool currentlyEnabled, bool enable)
     {
          //..Some stuff left out for brevity..
         }
 }

My problem is when I try to loop over resourceSpawnTimes in ResourceSpawnTiming's GetCurrentValue(..) method. The gen object is not null but it's List[] resourceSpawnTimes is! As illustrated by this screenshot:

alt text

Having tested I now know that Awake(), where resourceSpawnTimes should be intialised, in the ResourceGenerator is not called before OnInspectorGUI() in ResourceSpawnTimes, but I can't figure out anyway to initialise it earlier! I also tried OnEnable() but that didn't work either.. I believe the issue is somewhat complicated by the fact I need to know what has been entered into the editor interface for MatchInfo before deciding on a size..

Been experimenting without any luck for hours, help much appreciated!

capture7.png (19.2 kB)
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
0
Best Answer

Answer by Azrapse · Jan 16, 2014 at 05:42 PM

The problem is that you are initializing the array of lists, but not the lists themselves, so when you access an element of the array it is null.

Try initializing it like this:

 var arraySize = (int) (info.matchTimeSeconds / info.timeBetweenDropsSeconds);
 resourceSpawnTimes = new List<string>[arraySize];
 for(int i=0; i<arraySize; i++)
 {
   resourceSpawnTimes[i] = new List<string>();
 }

Or if you want a single, more elegant instruction, add using System.Linq; at the begining and initialize it like this:

 resourceSpawnTimes = Enumerable.Range(1, arraySize)
   .Select(i=>new List<string>())
   .ToArray();
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 MrsZorx · Jan 16, 2014 at 08:19 PM 0
Share

Thanks! I also found that I had to move the initialisation into a function i could call from onInspectorGUI() if the array was still null. Oh and I made the gen object Dirty although i'm not sure if that was necessary.

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

19 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

Related Questions

EditorApplication.projectWindowItemOnGUI and sub assets 1 Answer

Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer

[Answered]How to instantly draw rect in editor GUI for 2D tilemapper 1 Answer

Move Editor Button 1 Answer

EditorGUI.Popup Use of unnasigned local variable 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