Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ZeroKyun · Nov 02, 2015 at 07:00 PM · databasec# tutorialnot respondingstoring

Problems with database

Im following the Burgzerg Arcade course to make RPG items, and im having the next problems:

prntscr.com/8y9foh

prntscr.com/8y9iti

Dont know what to do because in second image it should be showing the 2 fields (Name and sprite) and not what is showing, and in the first image is not saving to database.

Second Code should look like this one: http://prntscr.com/8y9rmv

Here are my codes:

     using UnityEngine;
     using System.Collections;
     namespace Sakyadu.ItemSystem {
         [System.Serializable]
         public class ISTier : IISTier {
             [SerializeField] string _name;
             [SerializeField] Sprite _icon;
     
             public ISTier(){
                 _name = "Common";
                 _icon = new Sprite();
             }
             public string Name{
                 get {return _name;}
                 set {_name = value;}
             }
     
             public Sprite Icon{
                 get {return _icon;}
                 set {_icon = value;}
             }
         }
     }
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 namespace Sakyadu.ItemSystem {
 
     public class ISTierDatabase : ScriptableObject {
     
         //[SerializeField]
         public List<ISTier> database = new List<ISTier>();
     }
 }


The second one

    using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     namespace Sakyadu.ItemSystem {
     
         public class ISTierDatabase : ScriptableObject {
         
             //[SerializeField]
             public List<ISTier> database = new List<ISTier>();
         }
     }


The last one

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 namespace Sakyadu.ItemSystem.Editor {
     public class ISTierDatabaseEditor : EditorWindow {
 
         ISTierDatabase tierDatabase;
         ISTier selectedItem;
         Texture2D selectedTexture;
 
         const int SPRITE_BUTTON_SIZE = 46;
         const string FILE_NAME = @"sakyaduTierDatabase.asset";
         const string DATABASE_FOLDER_NAME = @"Database";
         const string DATABASE_FULL_PATH = @"Assets/" + DATABASE_FOLDER_NAME + "/" + FILE_NAME;
 
         [MenuItem("SakyaduEditor/Database/Tier Editor %#i")]
         public static void Init() {
 
             ISTierDatabaseEditor window = EditorWindow.GetWindow<ISTierDatabaseEditor> ();
             window.minSize = new Vector2 (200, 300);
             window.title = "Tier DB";
             window.Show ();
         }
 
         void OnEnable(){
 
             tierDatabase = AssetDatabase.LoadAssetAtPath (DATABASE_FULL_PATH, typeof(ISTierDatabase)) as ISTierDatabase;
             if (tierDatabase == null) {
 
                 if (!AssetDatabase.IsValidFolder("Assets/" + DATABASE_FOLDER_NAME))
                     AssetDatabase.CreateFolder("Assets", DATABASE_FOLDER_NAME);
 
                 tierDatabase = ScriptableObject.CreateInstance<ISTierDatabase>();
                 AssetDatabase.CreateAsset(tierDatabase,DATABASE_FULL_PATH);
                 AssetDatabase.SaveAssets();
                 AssetDatabase.Refresh();
             }
 
             selectedItem = new ISTier ();
         }
 
 
         void OnGUI(){
 
             AddTierToDetabase();
         }
 
         void AddTierToDetabase(){
             //Name
             selectedItem.Name = EditorGUILayout.TextField("Name:", selectedItem.Name);
             //Sprite
             if (selectedItem.Icon)
                 selectedTexture = selectedItem.Icon.texture;
             else
                 selectedTexture = null;
             if (GUILayout.Button (selectedTexture, GUILayout.Width (SPRITE_BUTTON_SIZE), GUILayout.Height (SPRITE_BUTTON_SIZE))) {
             
                 int controlerID = EditorGUIUtility.GetControlID(FocusType.Passive);    
                 EditorGUIUtility.ShowObjectPicker<Sprite>(null, true, null, controlerID);
             }
 
             string commandName = Event.current.commandName;
             if (commandName == "ObjectSelectorUpdated") {
             
                 selectedItem.Icon = (Sprite)EditorGUIUtility.GetObjectPickerObject();
                 Repaint();
             }
             if (GUILayout.Button ("Save")) {
 
                 if (selectedItem == null)
                     return;
 
                 tierDatabase.database.Add(selectedItem);
                 selectedItem = new ISTier();
 
             }
         }
     }
 }

Comment
Add comment · Show 4
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 Statement · Nov 02, 2015 at 11:11 PM 0
Share

in second image it should be showing the 2 fields (Name and sprite)

Yes, it should give you a dropdown so you can drill into Name and Sprite. In fact, Serialized classes should never be null in Unity. $$anonymous$$aybe you forgot to save the ISTier.cs after adding [System.Serializable]?

Actually, what type is IISTier? A plain interface? If it extends from UnityEngine.Object, then no, it won't work, because that will be a reference in Unity. Serializable classes should not extend from UnityEngine.Object.

I have my suspicions that you extend from ScriptableObject, $$anonymous$$onoBehaviour or some other Unity type.

avatar image ZeroKyun Statement · Nov 02, 2015 at 11:45 PM 0
Share

Yeah, but it doesnt show the drop down menu. Here is the IISTier script:

 using UnityEngine;
 using System.Collections;
 namespace Sakyadu.ItemSystem {
 
     public class IISTier : IISObject {
         string Name { get; set; }
         Sprite Icon { get; set; }
     }
 }


avatar image ZeroKyun ZeroKyun · Nov 02, 2015 at 11:46 PM 0
Share

And if needed, also here is the IISObject:

 using UnityEngine;
 using System.Collections;
 namespace Sakyadu.ItemSystem {
     public class IISObject : $$anonymous$$onoBehaviour {
         /*Global*/
         //Name
         string ISName { get; set; }
         //Pricing
         int ISPrice { get; set; }
         //Icon
         Sprite ISIcon { get; set; }
         //Tier
         ISTier ISTier { get; set; }
         //Weight
         int ISWeight { get; set; }
 
 
         /*Optional*/
         //Equipable Flag
         //Consumable Flag
         //Special Flag
         //Resistance
         //Take Damage
     }
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Nov 03, 2015 at 12:05 AM

You can't create new MonoBehaviours using a constructor. MonoBehaviours are Components and must be added to GameObjects. Unity handle instantiation of Component types. See AddComponent, Component.

Documentation: Note that your code will never directly create a Component. Instead, you write script code, and attach the script to a GameObject. See Also: ScriptableObject as a way to create scripts that do not attach to any GameObject.

 selectedItem = new ISTier();

This is invalid, because of the chain:

 public class ISTier : IISTier
 public class IISTier : IISObject
 public class IISObject : MonoBehaviour
 public class MonoBehaviour : Behaviour
 public class Behaviour : Component      
 public class Component : Object         < Docs say you cant create instances of this!
 public class Object : System.Object

ISTier is-a Component and must be created as belonging to a valid GameObject.

If you want to support polymorphic serialization, consider extending from ScriptableObject instead, and using ScriptableObject.CreateInstance. If you don't need to serialize references polymorphically, do not extend from any UnityEngine.Object type.

The last screenshot you showed bear the hallmark of a class that does not extend from UnityEngine.Object - unless there had been a custom propertydrawer made for it, to make a Unity reference appear and behave as a plain old serialized class.

The options I see:

Drop the inheritance chain to MonoBehaviour and either...

  • Consider not extending from any UnityEngine type to make a Serializable object

  • Consider extending from ScriptableObject type to make a Referenceable object

Comment
Add comment · 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

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

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

What is the best way to achieve local database for mobile? 0 Answers

MySQL not working when using the .exe on another computer 0 Answers

Non-relational DB on Android? 1 Answer

i need help unity get data from php 0 Answers

php $_POST is blank when I send variables from unity 3 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