Computer Science, asked by nt834629, 3 months ago

A list by default is ………………………​

Answers

Answered by Weeeeeeex
5

Explanation:

default list..........

Answered by dangding403
0

Answer:

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.

Add comment

2 Replies · Add your reply Sort:

avatar image

0

Best Answer

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()

};

}

}

Add comment · Hide 3 · Share

avatar imagesleepandpancakes · Feb 11, 2018 at 08:16 PM 0

Thanks for this. What happens if you ins$$anonymous$$d do value = 500; in OnAfterDeserialize() without a firstCall bool check?

avatar imageNoxury sleepandpancakes · Feb 11, 2018 at 08:25 PM 0

I switched out the functions because otherwise OnBeforeSerialize() will mostly be called even when you dont edit anymore which is resource consoming.

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.

avatar imagePizzaPie · Feb 11, 2018 at 09:39 PM 0

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!

Similar questions