• 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.

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

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

Multiple Cars not working 1 Answer

Editor Gui Problem 0 Answers

Distribute terrain in zones 3 Answers

EditorApplication.projectWindowItemOnGUI and sub assets 0 Answers

C# property keeps getting overwritten 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges