- Home /
Default value on List vanished
Is there any way to pass a default value to list-elements? I mean when I edit a list in edit mode and add an element, it should initialize its value to a custom default value, not 0.
[System.Serializeable]
public class Test {
int value = 500;
}
public class TestManager : MonoBehaviour {
public Test testInt;
public List<Test> testInts;
}
In the inspector, testInt.value has 500 as I wanted. But what if I want to have multiple Test Instances? Therefore I will use the list. But if I increase the size of the list, it will have their values set to 0, not 500.
I have tried to implement a constructor that should force them to serialize:
[System.Serializeable]
public class Test {
int value = 500;
public Test(){
Debug.Log("Iam called but the following line not?");
value = 500;
}
}
and this will be called whereever I add one Test-Instance to the list as the console shows the debug.Log. However it still does not initialize the value to 500. I dont know why, I mean this is such a simple task and it only works on single fields, not with lists.
I hope you understand what I mean.
Answer by Noxury · Feb 11, 2018 at 08:08 PM
Ok I found a solution in case somebody has the same problem. No need of cTor or ISerializableCallbackReceiver. This uses the Reset Callback (from the coq) that will also call at instanciation. Cheers!
[System.Serializeable]
public class Test {
int value = 500;
}
public class TestManager : MonoBehaviour {
public Test testInt;
public List<Test> testInts;
void Reset(){
testInts = new List<Test>(){
new Test()
};
}
}
Thanks for this. What happens if you ins$$anonymous$$d do value = 500;
in OnAfterDeserialize() without a firstCall
bool check?
I switched out the functions because otherwise OnBeforeSerialize() will mostly be called even when you dont edit anymore which is resource conso$$anonymous$$g.
The bool makes sure it will be called on the first time unity serialized this property, so this acts like a default value for single class fields like Test testInt above. If you leave the bool out, it will be resetted back to 500 everytime you want to reassign the value in the inspector.
That hardly solves anything, in case of object (class) field you can just use the default value declaration on class fields as you do above, as for lists this will fail (try to hit play and stop it). Regarding to this behaviour you maximize your chances to end up with bugs, data loss, on the long run and the benefits of it hardly overcome the cons.
Cheers!
Answer by jinincarnate · Mar 19, 2020 at 04:17 PM
For anyone looking for answer now: I ended up creating a button in my scriptable object that when clicked assigned values to its field automatically so that I don't have to manually.
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Item Database", menuName = "Create database/Item Database")]
public class ItemDatabase : ScriptableObject
{
public List<Item> itemList;
public bool Valid => itemList != null ? itemList.Count > 0 : false;
public Item FetchItemByID(int id)
{
for (int i = 0; i < itemList.Count; i++)
{
if (itemList[i].itemId == id)
return itemList[i];
}
return null;
}
public void AssignIds()
{
foreach(Item item in itemList)
{
item.itemId = itemList.IndexOf(item);
}
}
}
[System.Serializable]
public class Item
{
public int itemId;
public string itemName;
public string itemDescription;
public string slug;
public bool stackable;
public string itemType;
public GameObject itemPrefab;
public Sprite itemIcon;
public Item()
{
this.itemId = -1;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ItemDatabase))]
public class ItemDatabaseEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var script = (ItemDatabase)target;
if(script.Valid)
{
if(GUILayout.Button("Automatic Assign Ids", GUILayout.Height(40)))
{
script.AssignIds();
}
}
else
{
GUIStyle style = GUI.skin.box;
style.normal.textColor = Color.yellow;
style.fixedHeight = 40;
style.stretchWidth = true;
style.alignment = TextAnchor.MiddleCenter;
GUILayout.Box("Add Elements to the list to enable this button", style);
}
}
}
Answer by eladiozro · Jun 25, 2021 at 06:01 AM
What I did is this:
public class Example {
public float value_a = 1.0f;
public float value_b = 1.0f;
public Example(float a, float b)
{
value_a = a;
value_b = b;
}
}
This gives preset properties to the array when there is nothing in the component.
public Example [] inputs = new Example [] { new Example (0.5f, 0.5f) };
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Editor Script Reverts to Default State on Script Compilition 2 Answers
How do I make child classes in a list editable from the Inspector? 1 Answer
How to check if vector in array was not changed? 1 Answer
How to let multiple values contribute two one value constantly 1 Answer