- Home /
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:
TranslateModule: This handles the position of the object it is attached to.
RotateModule: This handles the rotation of the object it is attached to.
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":
After hitting "play":
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?
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.
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.
Your answer
Follow this Question
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