Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using SCRIPTABLE OBJECT between two scenes

Discussion in 'Scripting' started by Epic_Cube, Jul 22, 2019.

  1. Epic_Cube

    Epic_Cube

    Joined:
    Jul 3, 2012
    Posts:
    98
    Hi all,
    I've created a simple scriptable object: a list of items containing some strings. This data is edited in editor and used by a script in a scene
    scriptable object issue.png

    At Runtime, the script Message sequence reads data from the scriptable object and uses it. If I set the scene as the first scene of the game, everything works fine.
    Instead, if I start another scene and then I change scene, it looks that the reference to the scriptable object is lost (it's actually null).
    Is there an option to load scriptable object correctly when changing a scene?

    The scriptable object asset is placed in a common directory (no Resources, Plugins or similar) and I'm changing the scene using LoadLevel and LoadLevelAsync

    Here it's the scriptable object code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace AlexandrosShared.Data
    4. {
    5.     [System.Serializable]
    6.     [CreateAssetMenu( menuName = "Alexandros Shared/Data/Messages/Complex Message DB" )]
    7.     public class ComplexMessageDB : ScriptableObject
    8.     {
    9.  
    10.         #region Classes
    11.  
    12.         [System.Serializable]
    13.         public class ComplexMessage
    14.         {
    15.  
    16.             #region Fields
    17.  
    18.             public string Text = "";
    19.             public string ResourceAudio = "";
    20.             public string[] ResourceSprites = new string[0];
    21.             public float MinMessageDuration = 0;
    22.  
    23.             #endregion Fields
    24.  
    25.         }
    26.  
    27.         #endregion Classes
    28.  
    29.         #region Fields
    30.  
    31.         public string FullText = "";
    32.         public string Separator = ">>>";
    33.         public ComplexMessage[] Messages = new ComplexMessage[0];
    34.  
    35.         #endregion Fields
    36.  
    37.         #region Methods
    38.  
    39.         public ComplexMessage GetByText( string text )
    40.         {
    41.             return System.Array.Find( Messages, cm => cm.Text.ToLower() == text.ToLower() );
    42.         }
    43.  
    44.         void OnValidate()
    45.         {
    46.             string[] textLines = FullText.Split( new string[] { Separator }, System.StringSplitOptions.RemoveEmptyEntries );
    47.             for ( int i = 0; i < textLines.Length; i++ )
    48.                 textLines[i] = textLines[i].Trim( System.Environment.NewLine.ToCharArray() );
    49.  
    50.             if ( textLines.Length > Messages.Length )
    51.                 System.Array.Resize( ref Messages, textLines.Length );
    52.  
    53.             for ( int i = 0; i < textLines.Length; i++ )
    54.             {
    55.                 if ( Messages[i] == null )
    56.                     Messages[i] = new ComplexMessage();
    57.                 Messages[i].Text = textLines[i];
    58.             }
    59.         }
    60.  
    61.         #endregion Methods
    62.  
    63.     }
    64. }
     
  2. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    They will load when referenced, the error must be somewhere else.
    Are you sure the gameObject that throws the exception is the one you assigned the SO to?
     
  3. Epic_Cube

    Epic_Cube

    Joined:
    Jul 3, 2012
    Posts:
    98
    thx for reply.
    Yes I'm sure. The source of the error is the component with the reference to the scriptable object.
    picturemessage_yl20jesw.04m.png

    In OnEnable of my component I have:
    Code (CSharp):
    1.          void OnEnable()
    2.         {
    3.             if ( !MessageSource )
    4.             {
    5.                 Debug.LogError( "No Message Source specified" );
    6.                 return;
    7.             }
    8. ...