- Home /
Adding an item to a Generic List copies the old one
Hi, I have a public List containing a pair of information:
public List<EntityMethodPair> targets = new List<EntityMethodPair>();
The subclass for the EntityMethodPair class looks like this:
public class EntityMethodPair {
public Entity target;
public string method;
}
This works fine and I can add new items in the inspector, but, the old values always getting copied.
So if I put an entity named "a" in the target of item 0 and resize the list to 1, the new entry also will have "a" and the exact values like item 0.
Is there a trick so adding new things in the inspector will be empty things?
Annoying sometimes that - other times useful. I've not found a way of getting it to stop though ):
Answer by Jamora · Aug 09, 2013 at 01:05 PM
There is no way to get it to stop this behavior, apart from creating a custom inspector or property drawer for your list. The default inspector will always copy the old value.
Because it's so systematic, when you want empty indexes, just make sure the last value is of the type you want to duplicate before you increase the size. In this case the last value should be empty.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# how to create a Descending GUI List 1 Answer
Unity Editor Inspector creating gameObjects in world 2 Answers
Custom GUI Elements in Inspector 0 Answers