I am using scriptable objects to store presets for a game I’m making. When I dragged in the scriptable object into a serialize field, Unity kept returning NullReferenceExceptions. So I decided to debug it and print out the scriptable object variable.
The console looked like this:
So half the time the PlayerMoveset scriptable object is null, while the other half of the time it has a value.
How do I fix this?
Scriptable object code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Moveset ", menuName = "Custom Parameters/Player/Moveset")]
public class PlayerMovesetSO : ScriptableObject
{
[SerializeField] public string[] Moves;
[SerializeField] public float[] Amt;
}
This other script is supposed to read the values from the scriptable object.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMoves : MonoBehaviour
{
[SerializeField] private PlayerMovesetSO PlayerMovesetParameters;
[SerializeField] private PlayerMovement PlayerMovementScript;
[SerializeField] private float DashCooldown;
[SerializeField] private float DashAmt;
private PlayerInputActions PlayerActions;
private float MoveCooldown = 0f;
private float DashCounter = 0f;
private string[] Moves;
private bool HoldingPrimary = false;
private void Start()
{
PlayerActions = new PlayerInputActions();
PlayerActions.PlayerActions.Enable();
PlayerActions.PlayerActions.Primary.performed += PrimaryPerformed;
PlayerActions.PlayerActions.Primary.canceled += PrimaryCanceled;
}
private void PrimaryCanceled(InputAction.CallbackContext context)
{
HoldingPrimary = false;
}
private void PrimaryPerformed(InputAction.CallbackContext context)
{
HoldingPrimary = true;
}
private void Update()
{
MoveCooldown -= Time.deltaTime;
print(PlayerMovesetParameters);
}
}