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 lonely_cube · Aug 18, 2014 at 10:28 PM · c#listcustom inspector

How do I persist data from a custom inspector for a list? (C#)

Hello! In my project, I have an object with an EnemySpawner script on it. This script stores a list of EnemyInit objects, each of which stores a GameObject for the enemy itself, a Vector3 for its starting position, and a Bool to indicate whether it faces left or right at the start. I have set up a custom inspector for EnemySpawner, which allows me to input all of this data for each enemy, and also choose the number of enemies.

This all seems to be working, until I actually run the game. When I do, the data all resets to the defaults, with no GameObjects in the fields, (0, 0, 0) Vector3s, etc (although, oddly enough, the number of enemies remains correct). I assume this has something to do with me adding and removing elements from the list in the inspector script, but I don't know how else to change the number of enemies from the inspector. Does anyone know what I might try to solve this? Here are the two scripts:

EnemySpawner:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EnemyInit {
     public GameObject obj;
     public Vector3 start;
     public bool faceLeft;
 
     public EnemyInit(GameObject _obj, Vector3 _start, bool _faceLeft) {
         obj = _obj;
         start = _start;
         faceLeft = _faceLeft;
     }
 
     public EnemyInit() {
     }
 }
 
 public class EnemySpawner : MonoBehaviour {
     public int enemyNum;
 
     public List<EnemyInit> enemies = new List<EnemyInit>();
 
     // Use this for initialization
     void Start () {
         for(int i = 0; i < enemies.Count - 1; i++) {
             GameObject enemyCurrent = Instantiate ( enemies[i].obj, enemies[i].start, Quaternion.identity ) as GameObject;
             if ( enemies[i].faceLeft ) {
                 enemyCurrent.transform.RotateAround( transform.position, Vector3.up, 180 );
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 

EnemySpawnerEditor:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 
 [CustomEditor(typeof(EnemySpawner))]
 public class EnemySpawnerEditor : Editor 
 {
     public override void OnInspectorGUI()
     {
         EnemySpawner myTarget = (EnemySpawner)target;
 
         myTarget.enemyNum = EditorGUILayout.IntSlider ("Enemy Number", myTarget.enemyNum, 1, 10);
 
         while (myTarget.enemyNum > myTarget.enemies.Count) {
             myTarget.enemies.Add(new EnemyInit());
         }
         while (myTarget.enemyNum < myTarget.enemies.Count) {
             myTarget.enemies.RemoveAt(myTarget.enemies.Count - 1);
         }
 
         EditorGUILayout.Space ();
 
         for (int i = 0; i < myTarget.enemyNum; i++) {
             EditorGUILayout.Space ();
             EditorGUILayout.LabelField("Enemy " + (i + 1));
 
             myTarget.enemies[i].obj = (GameObject)EditorGUILayout.ObjectField ("Enemy Object", myTarget.enemies[i].obj, typeof(GameObject), false);
             myTarget.enemies[i].start = EditorGUILayout.Vector3Field ("Starting Position", myTarget.enemies[i].start);
             myTarget.enemies[i].faceLeft = EditorGUILayout.Toggle ("Facing Left", myTarget.enemies[i].faceLeft);
         }
     }
 }


Thank you to anyone who has an idea of what I could to to fix this. I have a feeling that I need to approach the whole thing in a different way, so that I don't have to change the list in the editor script, but I can't think of another way to do what I'm trying to do.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

How do you get a certain index of a List 2 Answers

Unity stops responding when i ask it to execute the same function for the third time 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