Why does my List overwrite itself when using .Add?

So I’m working on a GameDatabase editor for my project. I’ve created a custom asset from a ScriptableObject and a Weapon class that extends System.Object. The goal is simple: I want an editor window I set the data for the new weapon in and hit the add button and it will populate a weapon var in the editor window and assigning that to the database asset using .Add.

The problem is: When I use .Add and pass the newly set weapon it overwrites all previous weapons in the asset’s weapon list. Does anyone have any idea why? I can’t for the life of me figure this out.

I updated the script below to reflect the proper way to use .add with a list to add items via the editor window.

The Editor Window:

 class GameDatabase extends EditorWindow {
	//Weapon Variables
	var wepname : String = "Weapon Name";
	var description : String;
	var database : DatabaseClass;
	//Create Window
	static function Init () {
		var window:GameDatabase = EditorWindow.GetWindow(GameDatabase);

	function OnInspectorUpdate () {
		database = Resources.Load("database");
	function OnGUI () {
		//Weapon Properties
		GUILayout.Label ("Weapons", EditorStyles.boldLabel);
		wepname = EditorGUILayout.TextField ("Name", wepname);
		description = EditorGUILayout.TextField ("Description", description);
		//Button to Generate Weapon Data for Database
		if (GUILayout.Button ("Add Weapon to Database")) {
			var newWeapon = new Weapon();
			weapon.name = wepname;
			weapon.description = description;

It looks like you’re Loading database in AddWeapon(), changing it and flagging it as Dirty, but not actually saving it out. Next time you call AddWeapon() again you just re-load the original version.

AssetDatabase.CreateAsset(database, AssetDatabase.GetAssetPath(database);
This will save over your database with the new one.
You can call this instead of EditorUtility.SetDirty().

Call this after EditorUtility.SetDirty() to save everything that is dirty.

and the problem is most likely that you don’t create a new weapon when saving and it will overwrite the old one(because it is the same reference). Remove the weapon field from the top of the class and use a local variable in AddWeapon to store it; “var weapon: Weapon = new Weapon();”