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 m52nd-Architect · Mar 23, 2018 at 07:51 AM · editorinspectorarrayscustom editor

Why is my unity editor script not saving changes to the actual script?

I have written an editor script to modify a "Structure" script and for some reason when i change values it resets them to the default state when I hit play. have tried checking for overwriting of values and arrays and cannot find anything to give me a hint as to why. I have isolated he bug, however, to these two C# files and their interactions between each other.

EDITOR SCRIPT

 [CustomEditor(typeof(Structure))]
 public class StructureEditor : Editor {
     private int constructionResourcesNeeded;
     private bool showConstructionResources, showGhostNodes, showReferences = true, showTypeSpecificInfo, showSpecificReferences, showFloorVariations;
 
     private string[] resourceCostOptions = new string[] {
         "[0] Metal", "[1] Plastic", "[2] Food", "[3] Medicine" , "[4] Volume", "[5] Heat", "[6] Fuel", "[7] Water"
     };
 
     public override void OnInspectorGUI() {
         Structure structure = (Structure)target;
 
 
         showReferences = EditorGUILayout.Foldout(showReferences, "References");
         if (showReferences) {
             structure._controller = (GameController)EditorGUILayout.ObjectField("Game Controller", structure._controller, typeof(GameController), true);
             structure._cameraRay = (CameraRay)EditorGUILayout.ObjectField("Camera Ray", structure._cameraRay, typeof(CameraRay), true);
             structure.boundingBox = (Collider)EditorGUILayout.ObjectField("Bounding Box", structure.boundingBox, typeof(Collider), true);
 
             EditorGUILayout.Space();
 
             if (GUILayout.Button("Initialize Component")) {
                 structure.Initialize();
                 structure.approvedConnectionTypes[0] = true;
 
             }
         }
 
         EditorGUILayout.Space();
 
         structure.moduleName = EditorGUILayout.TextField("Module Name", structure.moduleName);
         structure.buidLevelIndex = EditorGUILayout.IntSlider("Floor Level", structure.buidLevelIndex, 1, 3);
         structure.UpdateFloorVariations();
 
 
 
         EditorGUILayout.Space();
 
         string[] options = Exodus.Data.StructureTypes;
         structure.structureType = EditorGUILayout.Popup("Type", structure.structureType, options);
 
         EditorGUILayout.Space();
 
         showFloorVariations = EditorGUILayout.Foldout(showFloorVariations, "Exterior Model Variations");
         if (showFloorVariations) {
             structure.FloorVariations[0] = (GameObject)EditorGUILayout.ObjectField("1st Floor Ext", structure.FloorVariations[0], typeof(GameObject), true);
             structure.FloorVariations[1] = (GameObject)EditorGUILayout.ObjectField("2nd Floor Ext", structure.FloorVariations[1], typeof(GameObject), true);
             structure.FloorVariations[2] = (GameObject)EditorGUILayout.ObjectField("3rd Floor Ext", structure.FloorVariations[2], typeof(GameObject), true);
 
         }
 
         EditorGUILayout.Space();
 
 
         EditorGUILayout.LabelField("Connection Types (" + structure.approvedConnectionTypes.Length + ")");
         EditorGUI.indentLevel++;
 
         //make sure the structure list of conenction matches the length of the actual connection types
         //if (structure.approvedConnectionTypes.Length != Data.StructureTypes.Length) {
         //    structure.approvedConnectionTypes = new bool[Data.StructureTypes.Length];
         //}
 
         for (int i = 0; i < Data.StructureTypes.Length; i++) {
             EditorGUILayout.BeginHorizontal();
             structure.approvedConnectionTypes[i] = EditorGUILayout.Toggle(structure.approvedConnectionTypes[i]);
             EditorGUILayout.LabelField("[" + i + "] " + Format.IntToStructureType(i));
             EditorGUILayout.EndHorizontal();
         }
         EditorGUI.indentLevel--;
 
 
         showConstructionResources = EditorGUILayout.Foldout(showConstructionResources, "Construction Resources");
         if (showConstructionResources) {
             EditorGUILayout.BeginHorizontal();
             if (GUILayout.Button("Add")) {
                 addConstructionResource(structure);
             }
             if (GUILayout.Button("Clear")) {
                 structure.constructionCost.Clear();
                 structure.constructionResource.Clear();
             }
             EditorGUILayout.EndHorizontal();
 
             for (int i = 0; i < structure.constructionResource.Count; i++) {
                 EditorGUILayout.BeginHorizontal();
                 structure.constructionResource[i] = EditorGUILayout.Popup(structure.constructionResource[i], resourceCostOptions);
                 structure.constructionCost[i] = EditorGUILayout.IntField("Cost", structure.constructionCost[i]);
                 if (GUILayout.Button("X")) {
                     structure.constructionCost.RemoveAt(i);
                     structure.constructionResource.RemoveAt(i);
                 }
                 EditorGUILayout.EndHorizontal();
                 EditorGUILayout.Space();
 
             }
         }
 
         EditorGUILayout.Space();
 
         #region Ghost Nodes
         //showGhostNodes = EditorGUILayout.Foldout(showGhostNodes, "Ghost Nodes
         //showGhostNodes = false;
         //if (showGhostNodes) {
         //    EditorGUILayout.BeginHorizontal();
         //    if (GUILayout.Button("Add")) {
         //        structure.ghostNodes.Add(new GameObject());
         //        GameObject tempGhostNode = structure.ghostNodes[structure.ghostNodes.Count - 1];
         //        tempGhostNode.transform.parent = structure.transform;
         //        tempGhostNode.name = "Ghost Node " + structure.ghostNodes.IndexOf(tempGhostNode);
         //        tempGhostNode.transform.SetPositionAndRotation(structure.transform.position, structure.transform.rotation);
         //        tempGhostNode.AddComponent<NodeVisualizer>();
         //    }
         //    if (GUILayout.Button("Clear")) {
         //        foreach (GameObject g in structure.ghostNodes) {
         //            DestroyImmediate(g);
         //        }
         //        structure.ghostNodes.Clear();
         //    }
         //    EditorGUILayout.EndHorizontal();
         //    for (int i = 0; i < structure.ghostNodes.Count; i++) {
         //        EditorGUILayout.BeginHorizontal();
         //        structure.ghostNodes[i] = (GameObject)EditorGUILayout.ObjectField("Ghost Node [" + i + "]", structure.ghostNodes[i], typeof(GameObject), true);
         //        if (GUILayout.Button("X")) {
         //            DestroyImmediate(structure.ghostNodes[i]);
         //            structure.ghostNodes.RemoveAt(i);
         //        }
         //        EditorGUILayout.EndHorizontal();
         //        EditorGUILayout.Space();
         //    }
         //}
 
         //EditorGUILayout.Space();
         #endregion
 
         showTypeSpecificInfo = EditorGUILayout.Foldout(showTypeSpecificInfo, "Type Specific Information (" + Format.IntToStructureType(structure.structureType) + ")");
         if (showTypeSpecificInfo) {
             EditorGUI.indentLevel++;
 
             showSpecificReferences = EditorGUILayout.Foldout(showSpecificReferences, "Specific References");
             if (showSpecificReferences) {
 
                 EditorGUI.indentLevel++;
                 #region SpecificReferences
                 //switch (structure.structureType) {
                 //    case 0:
                 //        Debug.LogWarning("No functionality suported with an structureType: Undefined!");
                 //        break;
                 //    case 1: // "WallSegment"
                 //        structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
 
                 //        break;
                 //    case 2:
                 //        // temp = "BoxHab";
                 //        structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
 
                 //        break;
                 //    case 3:
                 //        //temp = "Connector";
                 //        structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
 
                 //        break;
                 //    case 4:
                 //        // temp = "ConnectorModule";
                 //        structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
 
                 //        break;
                 //    default:
                 //        //temp = "Undefined";
                 //        break;
                 //}
                 #endregion
                 structure.snapNode = (Transform)EditorGUILayout.ObjectField("Parent Snap Node", structure.snapNode, typeof(Transform), true);
                 EditorGUI.indentLevel--;
             }
         }
         EditorGUI.indentLevel--;
 
 
     }
 
     void addConstructionResource(Structure _structure) {
         _structure.constructionResource.Add(0);
         _structure.constructionCost.Add(0);
     }
 }

STRUCTURE SCRIPT

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Structure : MonoBehaviour {
 
     [Header("References")]
     public GameController _controller;
     public CameraRay _cameraRay;
     public string moduleName;
     public Transform snapNode;
     private Structure parentStructure;
     private Structure[] connections;
     public Collider boundingBox;
 
     public GameObject[] FloorVariations = new GameObject[3];
 
     [Header("Data")]
     public int structureType = -1, buidLevelIndex = 0;
     public bool hasConnection, isConnected, underConstruction, needsRepair;
     [SerializeField] public bool[] approvedConnectionTypes = new bool[10];
 
     [Header("Costs & Resources")]
     public List<int> constructionResource;
     public List<int> constructionCost;
 
 
     //public List<int> resourceMod;
     //public List<int> modAmount;
 
     public List<GameObject> ghostNodes;
 
     private void Start() {
         Initialize();
         //grab all needed references even if it means searching for them in the scene
         if (_controller == null || _cameraRay == null) {
             Debug.LogWarning("UnassignedReferenceException : one or more of the reference variables was not set to an instance of an object.  " +
                 "Automatically searching and assigning proper references.");
             _controller = GameObject.FindObjectOfType<GameController>();
             _cameraRay = GameObject.FindObjectOfType<CameraRay>();
         }
 
         //assign a module name if none was provided
         if (moduleName == "" || moduleName == null) {
             moduleName = "Unnamed Module";
         }
 
         //process navigation and AI information here 
         #region AI stuff;
 
         #endregion;
 
 
         ////basic functionality per structure type to be carried out
         //switch (structureType) {
         //    case 0:
         //        Debug.LogWarning("No functionality suported with an structureType: Undefined!");
         //        break;
         //    case 1: // "WallSegment"
         //        break;
         //    case 2:
         //        // temp = "BoxHab";
         //        break;
         //    case 3:
         //        //temp = "Connector";
         //        break;
         //    case 4:
         //        // temp = "ConnectorModule";
         //        break;
         //    default:
         //        //temp = "Undefined";
         //        break;
         //}
 
         //process resources and modifiers
         _controller.modifyResource(0, 0, false);
 
 
         //modify model and meshes
 
 
         if (FloorVariations[0] != null) {
             foreach (GameObject variation in FloorVariations) {
                 variation.SetActive(false);
             }
             FloorVariations[buidLevelIndex].SetActive(true);
         }
     }
 
     //gat the type of structure from an integer
 
     public void snapTo(Transform snapPoint) {
         transform.SetPositionAndRotation(snapPoint.position, snapPoint.rotation);
 
     }
 
     public void snapToAndParent(Transform snapPoint) {
         snapTo(snapPoint);
         transform.parent = snapPoint.parent;
 
     }
 
     public void Initialize() {
         _controller = GameObject.FindObjectOfType<GameController>();
         _cameraRay = GameObject.FindObjectOfType<CameraRay>();
         //approvedConnectionTypes = new bool[Exodus.Data.StructureTypes.Length];
 
         if (_controller != null || _cameraRay != null) {
             Debug.Log("References applied.");
         }
         else {
             Debug.LogWarning("An error occured while trying to assign references.");
         }
     }
 
     public void UpdateFloorVariations() {
         if (FloorVariations[0] != null) {
             foreach (GameObject variation in FloorVariations) {
                 variation.SetActive(false);
             }
             FloorVariations[buidLevelIndex-1].SetActive(true);
         }
     }
 }
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 hexagonius · Mar 24, 2018 at 11:11 AM 0
Share

300 lines... isolated?

0 Replies

· Add your reply
  • Sort: 

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

106 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving editor-only variables 0 Answers

Overwrite the inspector window on Scene Asset heading chosen in the hierarchy window 0 Answers

How could I merge 2 components' inspectors through a custom editor? 2 Answers

Edit chosen material in the inspector for custom editor. 2 Answers

Customize arrays and classes in inspector window 2 Answers


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