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 alpeyek · Feb 18, 2012 at 07:25 PM · c#editorresetexecuteineditmode

Script variables filled in editor resetting in play mode

I've encountered a problem with Unity 3.5. So far I could write script that worked in editor and manipulated the values of its members (this is List actually). And after launching play mode the List data remained. But since I've installed Unity 3.5 that trick is over. Each time list is filled by script in editor mode and I start play mode the list data is erased and the count of list members goes to 0. But if I manually fill the list by references in Inspector then it runs OK. Here is the script when the list _neighbors is filled in editor when I build the scene and set to empty each time I trigger play mode.

 using UnityEngine;
 using System.Collections.Generic;
 
 [ExecuteInEditMode]
 public class Positioning : MonoBehaviour
 {
     public List<Cell> _neighbors = new List<Cell>();
     private Transform _transform;
     private Vector3 _lastPos;
     
     private CellManager _cellManager;
     
     public List<Cell> Neighbors
     {
         get     
         {
             return _neighbors;
         }
         set
         {
             _neighbors = value;
         }
     }
 
     void Awake()
     {
         _transform = transform;
         _cellManager = GameObject.Find("CellManager").GetComponent<CellManager>();
     }
 
     void Start()
     {
         _cellManager.AddCell(GetComponent<Cell>());
         _lastPos = _transform.position;
     }
     
     void Update()
     {
         if (_cellManager.Cells.Count > 1 &&
             Vector3.Distance(_transform.position, _lastPos) > 0.01f)
         {
             Cell nearestCell = _cellManager.NearestCell(_transform.position, gameObject.GetComponent<Cell>());
 
             if (Vector3.Distance(nearestCell.transform.position, _transform.position) < 1.0f)
             {
                 if (_neighbors.Count > 0)
                 {
                     foreach (Cell neighborCell in _neighbors)
                     {
                         if (neighborCell != gameObject.GetComponent<Cell>())
                         {
                             neighborCell.gameObject.GetComponent<Positioning>().Neighbors.Remove(GetComponent<Cell>());
                         }
                     }
                     _neighbors.Clear();
                 }
 
                 Vector3 pos =
                     nearestCell.GetFreeNeighborPlace(_transform.position);
                 _transform.position = pos;
                 _transform.rotation = nearestCell.transform.rotation;
 
                 List<Cell> neighbors = _cellManager.NearestCells(_transform.position, gameObject.GetComponent<Cell>());
 
                 foreach (Cell neighbor in neighbors)
                 {
                     neighbor.gameObject.GetComponent<Positioning>().Neighbors.Add(GetComponent<Cell>());
                     _neighbors.Add(neighbor);
                 }
             }
         }
 
         _lastPos = _transform.position;
     }
 }

I know that all the public members in scripts derived by MonoBehaviour are serialized and it worked before. I have no idea what is happened. Maybe I'm doing something wrong and I should go another way. Hope someone could help me with this. Thanks.

Comment
Add comment · Show 1
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 05:30 PM 0
Share

I have this problem too.. looking for answere as well

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by vbbartlett · Dec 14, 2012 at 11:01 PM

This seems to have fixed it I added if (GUI.changed) EditorUtility.SetDirty(target); To the OnInspectorGUI

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 austephner · Oct 18, 2016 at 07:19 PM 0
Share

this solution worked for me. However, I've never encountered this problem aside from this one instance ... which makes me worry about all the other times I haven't had this problem. Also makes me wonder why this particular component / class system I'm working on requires this. Surprised to see that this solution is from 2012 as well.

avatar image
0

Answer by Kryptos · Apr 21, 2012 at 06:38 PM

Add some Debug.Log.

I'm pretty sure the culprit is _neighbors.Clear(). Since your script is executed in edit mode, there might be some condition that cause this part of Update to be called once the game start.

Comment
Add comment · 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
0

Answer by ruben_hiet · Jan 11, 2013 at 10:32 AM

If you have a list that is containing classes you need to add [System.Serialize] on top of your class like this :

[System.Serialize] public List Neighbors { get
{ return _neighbors; } set { _neighbors = value; } }

variables are standard serializable but classes aren't even if you set public infront you need to add this on top to let your list remember its class variables. hope it helps

Comment
Add comment · 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

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

10 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Updating object on inspector value changes in editor 1 Answer

Getting GUI.skin in editor script changed entire Unity Editor GUI 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