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.
EDIT
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;
//Database
var database : DatabaseClass;
//Create Window
@MenuItem("Window/GameDatabase")
static function Init () {
var window:GameDatabase = EditorWindow.GetWindow(GameDatabase);
window.Show();
}
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;
database.weapons.Add(weapon);
EditorUtility.SetDirty(database);
}
}
}