Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Why does adding a value to list<> makes list not editable in unity inspector

Discussion in 'Scripting' started by Rewaken, Oct 4, 2018.

  1. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    34
    I am adding elements from the first list to another similar list, but it makes first not editable in the Unity Inspector. Whenever I try to change values from the inspector it makes them again 0. While If I comment list.Add() I can change values in the inspector.
    Code (CSharp):
    1. [Serializable]
    2. public class Characterz
    3. {
    4.   public string Name;
    5.   public int Age;
    6.   public float Gold;
    7. }
    8.  
    9.  
    10.  
    11.  
    12. public class Actor: MonoBehaviour
    13. {
    14.      public List<Characterz> kings=  new List<Characterz>();
    15.  
    16.     public List<Characterz> Actors = new List<Characterz>();
    17.  
    18.      void Start()
    19.      {
    20.         for (int y = 0; y < 30; y++)
    21.          {
    22.              Actors.Add(new Characterz());
    23.              if (y < 4)
    24.              {
    25.  
    26.                  Actors[y].Position = Characterz.PositionType.King;
    27.                 //I need to comment following line to change Actors
    28.                 // list value in editor
    29.                  kings.Add(Actors[y]);
    30.              }
    31.  
    32.          }
    33.       }
    34.  
    35. }
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    53
    Have you tried doing this without serializing the character class, just to see if its the serialization that is affecting the inspector?
     
  3. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    34
    Yeah I serailization affecting inspector, Removing all ref from class solved the issue
     
    Cyber-Dog likes this.
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    53
    Ok great :)

    If you can explain why you want to serialise the characters and how you will use it, I can help with finding a solution.
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    373
    That line makes an item in the second list _point to_ something in the first. Unity's serializer doesn't understand the concept of Actors and Kings both having a reference to the same Character object. It needs for you to own all of your objects.

    "But wait", you say, "I can save references to gameObject and scripts." That's because Unity flips the rules for them -- it assumes you only want to save a reference, since Unity always owns those anyway.
     
  6. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    34
    Hey,
    I believe it is caused by it multiple serializable lists. A class is used by reference, when I show it multiple times in the inspector, the memory shared, but Unity uses a different object to do serialization. This is becomes a mess, and probably Unity decided to just let stop changing values only change in one direction.
     
  7. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    53
    I might have to chuck this into Unity when I get home from work :)

    Characterz.PositionType.King; - Were do you get the PositionType.King, I dont recognize this as a unity function, or see it as a declared variable?
     
  8. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    34
    Well sorry before posting code here I edited all non relevent part of the code. Position is enum in class Characterz
     
    Cyber-Dog likes this.
  9. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    53
    Hey, I threw it in and didn't notice anything odd. Use my code and tell me if its not doing something that you wanted..

    One thing I did, is I broke up the script. I feel the "Character" class should be separate, especially if your going to serialize.

    Code (CSharp):
    1. //using System.Collections;
    2. //using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Character
    7. {
    8.     public string name;
    9.     public int age;
    10.     public float gold;
    11. }
    Code (CSharp):
    1. //using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterManager : MonoBehaviour {
    6.  
    7.     public List<Character> kings = new List<Character>();
    8.     public List<Character> actors = new List<Character>();
    9.  
    10.     void Start () {
    11.         for (int y = 0; y < 30; y++)
    12.         {
    13.             actors.Add(new Character());
    14.             if (y < 4)
    15.             {
    16.                 kings.Add(actors[y]);
    17.             }
    18.  
    19.         }
    20.     }
    21. }
    22.  
     
  10. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    34
    Hey, even if we use this we can not change value of Age in Inspector(in kings list) as referances are already passed, best way is to use structs.