Sunday, December 4, 2022
HomeGame Developmentunity - NullReference exception with a singleton enter supervisor

unity – NullReference exception with a singleton enter supervisor


I’ve been utilizing the brand new enter system to simulate contact in my sport, it really works completely however solely with the enter supervisor(which is a singleton for the benefit of use), once I add the PlayerMover script it returns me nullReference. The error seems in two scripts on the identical time. The enter supervisor is within the scene.

enter image description here

InputManager script(with default execution order of -1):

public class InputManager : Singleton<InputManager>
{
    non-public PlayerInput _playerInput;
    public Motion<Vector2> _OnScreenTouched;
    non-public void Awake()
    {
        _playerInput = new PlayerInput();
    }
    non-public void OnEnable()
    {
        _playerInput.Allow();
    }
    non-public void OnDisable()
    {
        _playerInput.Disable();
    }
    non-public void Begin()
    {
        _playerInput.Participant.Flip.carried out += ctx => ScreenTouched(ctx);

    }
    non-public void ScreenTouched(InputAction.CallbackContext context)
    {
        _OnScreenTouched?.Invoke(_playerInput.Participant.Flip.ReadValue<Vector2>());
        
    }
}

Singleton class:

public class Singleton<T> : MonoBehaviour the place T : Part
{
    non-public static T _instance;
    public static T Occasion
    {
        get
        {
            if (_instance == null)
            {
                var objs = FindObjectOfType(typeof(T)) as T[];
                if (objs.Size > 0)
                    _instance = objs[0];
                if (objs.Size > 1)
                    Debug.LogError("There's a couple of" + typeof(T).Title + "within the scene.");
                if(_instance == null)
                {
                    GameObject obj = new GameObject();
                    obj.hideFlags = HideFlags.HideAndDontSave;
                    _instance = obj.AddComponent<T>();
                }
            }
            return _instance;
        }
    }
}
public class SingletonPersistent<T> : MonoBehaviour the place T : Part
{
    public static T Occasion { get; non-public set; }
    public digital void Awake()
    {
        if (Occasion == null)
        {
            Occasion = this as T;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(gameObject);
        }

    }
}

PlayerMovement script:

public class PlayerMovement : MonoBehaviour, IMovable
{
    [SerializeField] non-public float _movementAmmount;
    non-public InputManager _inputManager;
    non-public Digicam _camera;

    non-public void Awake()
    {
        _inputManager = InputManager.Occasion;
        _camera = Digicam.major;
    }
    non-public void OnEnable()
    {
        _inputManager._OnScreenTouched += Transfer;
    }
    non-public void OnDisable()
    {
        _inputManager._OnScreenTouched -= Transfer;
    }

    public void Transfer(Vector2 screenPosition)
    {
        if (screenPosition.x <= Display.width / 2)
        {
            rework.place -= new Vector3(_movementAmmount,0,_camera.nearClipPlane);
        }
        if (screenPosition.x > Display.width / 2)
        {
            rework.place += new Vector3(_movementAmmount, 0, _camera.nearClipPlane);
        }
    }
}

Interface for PlayerMovement:

public interface IMovable
{
    void Transfer(Vector2 screenPosition);
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments