Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thellama · Oct 04, 2012 at 04:03 PM · listassetscriptableobjecteditor window

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.

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);
         }
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Broheim · Oct 04, 2012 at 04:13 PM 0
Share

function AddWeapon() { database = Resources.Load("database"); // more code... }

Doesnt this line reset your database class every time you call AddWeapon? Shouldnt you initiate the database only once?

avatar image thellama · Oct 04, 2012 at 04:20 PM 0
Share

But shouldn't loading the reference from the resources load the previously saved version and not overwrite it? And from my understanding .Add should be appending a new object to the end of the list and not overwriting all the weapons from 0 and up.

avatar image Broheim · Oct 04, 2012 at 04:36 PM 0
Share

You understand .Add correct. Still the database gets reset because unity cannot serialize generic lists in the first place.

avatar image Zerot · Oct 04, 2012 at 04:43 PM 0
Share

Unity can serialize generic lists just fine. The only thing it has problems with is serializing lists with subclasses. for example, you have a class called Laser that extends Weapon and you store that in a list of Weapon, then it won't get deserialized properly. it will deserialize as Weapon ins$$anonymous$$d of Laser

avatar image Broheim · Oct 04, 2012 at 05:03 PM 0
Share

Thanks for cleaning that up, Zerot.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Zerot · Oct 04, 2012 at 04:47 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thellama · Oct 04, 2012 at 04:55 PM 0
Share

Just figured that out Zerot when my friend came to help. Such a stupid mistake. Thank you.

avatar image
1

Answer by JRule4 · Oct 04, 2012 at 05:08 PM

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.

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

Or: AssetDatabase.SaveAssets() Call this after EditorUtility.SetDirty() to save everything that is dirty.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thellama · Oct 04, 2012 at 05:46 PM 0
Share

I did some tests and I like this method you mentioned more, keeps my data safe from crashes and moving assets from project to project. Is there any real difference from SetDirty() and CreateAsset() if you just SaveAssets() right after SetDirty()?

avatar image JRule4 · Oct 04, 2012 at 05:49 PM 0
Share

The main difference is that SetDirty() + SaveAssets() will save everything that is dirty, whereas using CreateAsset() will just save that one asset.

avatar image thellama · Oct 04, 2012 at 06:11 PM 0
Share

Thanks for the info but it seems like CreateAsset() will not override and already existing asset. :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Auto removing entries form a scriptableobject list when asset is deleted. 0 Answers

A node in a childnode? 1 Answer

Load ScriptableObject / asset file and save as object to list 1 Answer

How to reference variable in ScriptableObject(.asset files)? 1 Answer

Prefab as a subasset of a ScriptableObject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges