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 Amit · Apr 21, 2012 at 04:05 PM · editorlistvariableoninspectorgui

Problem keeping List variables saved when triggering play button

I have made an Editor script (inspector) to a target script called BossScript. the BossScript have 2 vairables of type List< Obsticles>, and List< bool>. Im modifying these variables from the editor script. it all works fine till i trigger the play button and then the variables reset... (like if i had 5 objects on the list, it gets to 0). It worked fine before the last update tho, i used to do it all the time. i tried something with the Serialization but it didn't work either...

Editor script:

 using UnityEngine; 
 using UnityEditor; 
 using System.Collections; 
 using System.Collections.Generic; 
 
 [CustomEditor(typeof(BossScript))] 
 
 public class BossScript_Editor : Editor 
 { 
     public override void OnInspectorGUI() 
     { 
         BossScript t = target as BossScript; 
 
         GUI.color = Color.green; 
         if (GUILayout.Button("Add new obsticle")) 
         { 
             t.obsticles.Add(new Obsticle()); 
             t.showObsticles.Add(false); 
         } 
 
         EditorGUILayout.Space(); 
         EditorGUILayout.Space(); 
 
         GUI.color = Color.white; 
         for (int i = 0; i < t.obsticles.Count; i++) 
         { 
             t.showObsticles[i] = EditorGUILayout.Foldout(t.showObsticles[i], ("Obsticle " + i.ToString()));
             if(t.showObsticles[i]) 
             { 
                 EditorGUILayout.LabelField("Properties:"); 
 
                 t.obsticles[i].time = EditorGUILayout.FloatField("Time:", t.obsticles[i].time);
 
                 t.obsticles[i].isCurrentObjectPosition = EditorGUILayout.Toggle("Current position?", t.obsticles[i].isCurrentObjectPosition);
                 if (t.obsticles[i].isCurrentObjectPosition) 
                 { 
                     t.obsticles[i].pos = EditorGUILayout.Vector3Field("Offset:", t.obsticles[i].pos);
                 } 
                 else 
                 { 
                     t.obsticles[i].pos = EditorGUILayout.Vector3Field("Position:", t.obsticles[i].pos);
                 } 
 
                 t.obsticles[i].obj = (GameObject)EditorGUILayout.ObjectField(t.obsticles[i].obj, typeof(GameObject));
 
                 GUI.color = Color.red; 
                 if(GUILayout.Button("Delete Obsticle")) 
                 { 
                     t.obsticles.RemoveAt(i); 
                     t.showObsticles.RemoveAt(i); 
                 } 
                 GUI.color = Color.white; 
 
                 EditorGUILayout.Space(); 
 
                 EditorGUILayout.LabelField("Effects"); 
 
                 EditorGUILayout.Space(); 
             } 
         } 
 
         if (GUI.changed) 
         { 
             EditorUtility.SetDirty(target); 
         } 
     } 
 }  

BossScript.cs

 using UnityEngine; 
 using System.Collections; 
 using System.Collections.Generic; 
 
 public class BossScript : MonoBehaviour 
 { 
     public List<Obsticle> obsticles = new List<Obsticle>(); 
     public List<bool> showObsticles = new List<bool>(); 
 
     void Awake() 
     { 
         StartCoroutine(SpawnObsticles()); 
     } 
 
     private IEnumerator SpawnObsticles() 
     { 
         float startTime = Time.time; 
         float lastInstantiationTime; 
 
         yield return new WaitForSeconds(obsticles[0].time + startTime); 
 
         lastInstantiationTime = Time.time; 
 
         for (int i = 0; i < obsticles.Count - 1; i++) 
         { 
             print(i + " TIME: " + Time.time); 
             yield return new WaitForSeconds(obsticles[i + 1].time - lastInstantiationTime + startTime);
 
             lastInstantiationTime = Time.time; 
         } 
     } 
 } 
 
 public class Obsticle 
 { 
     public float time; 
     public bool isCurrentObjectPosition; 
     public Vector3 pos; 
     public GameObject obj; 
 }  
Comment
Add comment · Show 5
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 Owen-Reynolds · Apr 21, 2012 at 08:33 PM 0
Share

Were they being perma-saved before? You could edit them, quit Unity, restart, and there they were again? (as opposed to only being saved within the same session.)

avatar image Amit · Apr 21, 2012 at 08:49 PM 0
Share

Nope, after I restart unity, they reset too..

avatar image Owen-Reynolds · Apr 22, 2012 at 07:38 AM 0
Share

Then that's it. Unity wants to save after you edit, then read on Start. It was only a fluke that it sort-of worked before. At the least, you'll need to add [System.serializable] in front of your Obstacle class (so C# knows to save it.)

Plus I'm not positive Unity will save lists -- if it isn't saving your bool List, for example. You may have to add an array. copy the list in as you Edit, and read into the List on Start.

avatar image Amit · Apr 22, 2012 at 01:02 PM 0
Share

But it worked before.. why would they change it O_o And ill check it soon

avatar image Owen-Reynolds · Apr 22, 2012 at 04:17 PM 0
Share

If they weren't saved when you quit the Editor, it really wasn't working. You have to close Unity sometime, and I imagine the builds always need data saved, so never worked. Even if you never upgraded Unity, you would have had to fix it, eventually.

The fact it sort of worked was a "bug in your favor." Like if a checkout guy never checks your ID for beeer. When they start carding, you can't complain about always getting underage beer before.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Kryptos · Apr 22, 2012 at 04:29 PM

You cannot save values that are not serializable. Try with this:

 [System.Serializable]
 public class Obstacle 
 { 
     public float time; 
     public bool isCurrentObjectPosition; 
     public Vector3 pos; 
     public GameObject obj; 
 }

And by the way, the correct spelling is Obstacle. Because obsticle means the female testicle and I don't think it is what you mean here.

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 Amit · Apr 22, 2012 at 05:16 PM 0
Share

LOL XDDD sorry and thank you for fixing and it works thank you ^^

avatar image
0

Answer by Owen-Reynolds · Apr 21, 2012 at 06:04 PM

Try commenting out the two new's in BossScript.

In my limited EditorScript experience, not in 3.5, I believe my lists were always zeroed out on running until I made code look like the standard Inspector: have BossScript only declare List<Obsticle> obsticles; and move the "new" to the Editor script: if(t.obsticles==null) new it. In your case, you wouldn't need that second part until you started on a fresh BossScript.

Plus, this is a good chance to practice Search and Replace (^H obsticle obstacle)

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 Amit · Apr 21, 2012 at 06:17 PM 0
Share

Thanks, but unfortuently it didnt work...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add trasform to list using editor gui. 1 Answer

A node in a childnode? 1 Answer

How to add objects in Inspector 1 Answer

Why does this return a NullReferenceException? 0 Answers

Variable will not change? 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