Search Unity

ScriptableObject value = 0

Discussion in 'Scripting' started by Katasa, Dec 14, 2014.

  1. Katasa

    Katasa

    Joined:
    Oct 26, 2014
    Posts:
    7
    Hello,

    To have a simple reading for you, I don't show you get/set.

    I have a folder "Maps". In this folder, I have some asset created by this code :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. public class WorldCreation
    6. {
    7.     [MenuItem("Assets/Custom Assets/World")]
    8.     public static void CreateNewWorld()
    9.     {
    10.         CustomAssetUtility.CreateAsset<World>();
    11.     }
    12. }
    13.  
    World is a ScriptableObject :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class World : ScriptableObject
    7. {
    8.     [SerializeField]
    9.     private int _worldX;
    10. }
    World have a CustomInspector :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using System.Collections.Generic;
    5. using System;
    6. using System.Diagnostics;
    7.  
    8. [CustomEditor(typeof(World)), CanEditMultipleObjects]
    9. public class WorldInspector : Editor
    10. {
    11.    World world_class;
    12.  
    13. Void OnEnable()
    14. {
    15.    world_class = (World)target;
    16.    UnityEngine.Debug.Log(world_class.worldX);
    17. }
    18.  
    19. public override void OnInspectorGUI()
    20. {
    21.    world_class.worldX = EditorGUILayout.IntField(world_class.worldX);
    22.    EditorUtility.SetDirty(world_class);
    23. }
    Well, even if I use SetDirty, on Debug.log, my value is 0 when I close and re-open Unity.

    I did the same thing with the maner (FindProperty), and everything was ok, but I had an other problem, I was not able to access to my Array of class I have in my World Script.

    So, I have 2 questions :

    - Why my worldX in 0 in OnEnable when I close and re-open Unity, despite I initialized it on CustomInspector ?
    - How can I get the array of class (define in World script) with FindProperty ?

    Thanks for your answer !
     
    Last edited: Dec 14, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Does "CustomAssetUtility.CreateAsset<World>();" create a new file, create a scriptable object and add it to this file? If now, that is the problem. And keep in mind that all of that is needed, otherwise you are going to have issues.

    What exactly was the problem you were having with arrays when using serialized object/property?
     
  3. Katasa

    Katasa

    Joined:
    Oct 26, 2014
    Posts:
    7
    Hello,

    Thanks for your help. To create my ScriptableObject asset, I use this code (from the course "Introduction to ScriptableObject" on live training) :
    Code (CSharp):
    1.     [MenuItem("Assets/Gameobject/Custom Assets/World")]
    2.     public static void CreateWorld () {
    3.    
    4.         var my_world = ScriptableObject.CreateInstance(typeof(World));
    5.  
    6.         AssetDatabase.CreateAsset(my_world, "Assets/new_world.asset");
    7.         AssetDatabase.SaveAssets();
    8.         EditorUtility.FocusProjectWindow();
    9.         Selection.activeObject = my_world;
    10.  
    11.         Debug.Log(AssetDatabase.GetAssetPath(my_world));
    12.     }
    I tried something else, but I always have the same result : leave and re-open Unity reset my assets.

    For the array, the problem is :

    In my World.cs, I have public Cube[] cubes (where Cube is a class with int, list<Vector3>, and others variables). I don't find any method to access to this array with SerializedProperty class.

    I try two differents maner to use my ScriptableObject :
    Code (CSharp):
    1. - World world = (World)target
    2. - SerializedObject world = new SerializedObject(target);
    (which use "SerializedProperty class")

    Someone told me that was better to use SerializedObject(target) than (World)target.

    Thanks for your help !

    Regards
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  5. Katasa

    Katasa

    Joined:
    Oct 26, 2014
    Posts:
    7
    Hello,

    I don't find any function to take and use my array on SerializedProperty, it was my problem. I tried to cast "Object" in "Cube[]", but this doesn't work. Pehapse I have to use a "while" and the function GetArrayElementAtIndex. I will try.

    And for my asset which not save my data, have you an idea ? Is it a problem with my asset creation ?

    Regards !
     
    Last edited: Dec 20, 2014
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That's exactly what you need to do.

    I couldn't find an obvious issue in the asset creation script. You need to stick to one approach. Either SerializedObject or not and then go with that one consequently. Try to just have a test value, like an int in your scriptable object and make sure this one is correctly stored. If it is not preserved, show the code for the scriptable object and the corresponding editor class.