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 /
avatar image
0
Question by ZeBarba · Sep 23, 2019 at 02:49 PM · arraysscriptableobject

Array of scriptable Objects inside scriptable Object reseting on Play

Hello there.

I'm toying around with scriptable objects. There is one problem I can't get around, which is this:

I have three kinds of scriptable objects:

  1. TranslateModule: This handles the position of the object it is attached to.

  2. RotateModule: This handles the rotation of the object it is attached to.

  3. ObjectState: This stores an array of TranslateModules and another array of RotateModules.

The thing is: On editor mode I drag the instances of the Translate modules and Rotate modules to the Object state arrays, but once I click play, they all gets deleted, causing several null references during game play.

I'm aware that unity is unable to serialize 2 dimensional arrays, does the same principle applies here? I did put [System.Serializable] in all classes, and even tried [serizliazeField] even on public variables.

Here is some code: The TranslateModule is a class derived from an abstract class:

     [System.Serializable]
     public abstract class TranslateModule : ScriptableObject
     {
         [HideInInspector] public ModularCameraController cameraController;
         [HideInInspector] public Vector3 TranslateOutput;
 
         [Header("Basic settings (base class)")]
         [HelpBox("This module is the basic TRANSLATE module.\n" +
             "It MUST provide a Vector3 with the amount of movement for ONE update ONLY.\n" +
             "If authoritarian methods are used in this module, they will potencially over write all the other modules inputs or \n" +
             "cause some undesired behaviour"
             ,HelpBoxType.None)]
         [Tooltip("If disabled, the Camera Controller will ignore the output of this module\n")]
         public bool enableModule = true;
         [Space]
         [Tooltip("Determines if the tranlation will be applied for the X axis")]
         public bool applyToX = true;
         [Tooltip("Determines if the tranlation will be applied for the Y axis")]
         public bool applyToY = true;
         [Tooltip("Determines if the tranlation will be applied for the Z axis")]
         public bool applyToZ = true;
 
 
 
         public bool IsEnabled()
         {
             return enableModule;
         }
 
         public abstract void StartModule();
         public abstract void RunModule();
         public abstract void SetEnabled();
         public abstract void SetDisabled();
     }

Here is the TranslateModule used:

 [CreateAssetMenu(fileName = "TranslateWithMainObj", menuName = "Camera Controller Module/SampleModules/TranslateModules/TranslateWithMainObj")]
     public class TranslateWithMainObj : TranslateModule
     {
         private Vector3 lastPosition;
         private Vector3 currentPosition;
         [Header("Specific Settings")]
         [SerializeField, TextArea]
         private string SpecificModuleDescription;
         [Tooltip("If marked, in case the module is disabled and then enabled at run time, it will reposition the camera close the to camera. Otherwise, it will simply restart the translation effect whereever the camera is")]
         public bool alwaysClamp;
         
 
         public override void StartModule()
         {
             lastPosition = GetMainObjectPosition();
             currentPosition = GetMainObjectPosition();
         }
 
         public override void RunModule()
         {
             UpdateObjectPosition();
             TranslateOutput = currentPosition - lastPosition;
         }
 
         public override void SetEnabled()
         {
             if (!alwaysClamp)
             {
                 currentPosition = GetMainObjectPosition();
                 UpdateObjectPosition();
             }
             enableModule = true;
         }
 
         public override void SetDisabled()
         {
             enableModule = false;
         }
 
         private Vector3 GetMainObjectPosition()
         {
             return cameraController.mainObject.transform.position;
         }
 
         private void UpdateObjectPosition()
         {
             lastPosition = currentPosition;
             currentPosition = GetMainObjectPosition();
         }
     }

This is the scriptable object number 3 (ObjectState)

     [System.Serializable]
     [CreateAssetMenu(fileName = "CameraState", menuName = "Camera Controller Module/CameraStates")]
     public class CameraState : ScriptableObject
     {
         
         [SerializeField] public CameraStates stateName;
         [SerializeField] public TranslateModule teste;  //<--- used for testing. When not in an array, it is saved.
         [SerializeField] public TranslateModule[] translateModules;
         [SerializeField] public RotateModule[] rotateModules;
     }

After that, this last object posted above (CameraState) is a variable inside the CameraController script, which performs read-only method on it.

Any thoughts?

Before hitting "play": alt text

After hitting "play": alt text

image-2.png (5.9 kB)
image-1.png (7.1 kB)
Comment
Add comment · Show 6
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 WarmedxMints · Sep 23, 2019 at 03:01 PM 0
Share

Creating a variable in a scriptable object doesn't change if Unity can serialize something. It can certainly serialize references to other SO's in your project folder though. Show some code. What class is the translate modules and rotate modules?

avatar image ZeBarba WarmedxMints · Sep 23, 2019 at 03:33 PM 0
Share

I updated the question with the scripts. Thank you. I also tested it without the use of array, just a "regular" variable. In this case, the does serialize the SO. It looks like something related to the use of arrays.

avatar image WarmedxMints ZeBarba · Sep 23, 2019 at 04:15 PM 0
Share

You don't need the Serializable attribute on Something which inherits from ScritableObject nor do you need SerializedField on a public field. Which version of Unity are you using? I store Lists of SO's in So's without any issues and I just tried something similar to what you are doing with no issues at all.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

118 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 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 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 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

Is there a way to optimize working with arrays? 3 Answers

Unable to serialize my list in a Unity Custom Editor script. 1 Answer

Trying to assign UI images from an array with scriptable objects not working? 1 Answer

Implementing a 2D array as a field on a ScriptableObject? 1 Answer

How can I find all instances of a Scriptable Object in the Project (Editor) 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