Help with Error Code CS0019
So this excerpt of code is from my character controller script on line 34.
private ControllerParameters2D Parameters { get { return _overrideParameters ?? DefaultParameters; } }
I'm getting a Error Code CS0019, stating, "Operator '??' cannot be applied to operands of type 'ControllerParameters2D' and 'ControllersParameters2D'"
I know this is also referring to the ControllerParameters2D script I made. The code is here below:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class ControllersParameters2D
{
public enum JumpBehavior
{
CanJumpOnGround,
CanJumpAnywhere,
CantJump
}
public Vector2 MaxVelocity = new Vector2(float.MaxValue, float.MaxValue);
[Range(0, 90)]
public float SlopeLimit = 30;
public float Gravity = -25f;
public JumpBehavior JumpRestrictions;
public float JumpFrequency = .25f;
}
I'm very new to C# coding. I even copied this from the 9th video of the Creating a 2D Game in Unity 4.5 tutorial at Udemy. I know this code was originally for an older version of Unity and since I'm using Unity 5.6.1f1, I'm sure that some of the code is incompatible with the newer version.
Please help me and explain to me in depth the logic behind it.
Then you ned to explain what _overrideparameters and DefaultParameters are.
Answer by Tekki-777 · Jun 21, 2017 at 11:54 AM
I did. I should've posted that part of the code in the first place. :/
public LayerMask PlatformMask;
public ControllersParameters2D DefaultParameters;
public ControllerState2D State { get; private set; }
private readonly Vector2 velocity;
public Vector2 GetVelocity()
{
return velocity;
}
public bool CanJump { get { return false; } }
public bool HandleCollisions { get; set; }
private ControllerParameters2D Parameters { get { return _overrideParameters ?? DefaultParameters; } }
private Vector2 _velocity;
private Transform _transform;
private Vector3 _localScale;
private BoxCollider2D _boxCollider;
private ControllerParameters2D _overrideParameters;
Is ControllersParameters2D a struct? If so, it won't work, it has to be a class ins$$anonymous$$d because it cannot be null.
Its already a public class, as seen with the ControllerParameters2D script. Here's the code (again).
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class ControllersParameters2D
{
public enum JumpBehavior
{
CanJumpOnGround,
CanJumpAnywhere,
CantJump
}
public Vector2 $$anonymous$$axVelocity = new Vector2(float.$$anonymous$$axValue, float.$$anonymous$$axValue);
[Range(0, 90)]
public float SlopeLimit = 30;
public float Gravity = -25f;
public JumpBehavior JumpRestrictions;
public float JumpFrequency = .25f;
}
Answer by hexagonius · Jun 22, 2017 at 11:49 AM
The problem is a mix of different classes here:
ControllerParameters2D and ControllersParameters2D
DefaultParameters is the latter where the return type of the property and _overrideParameters is the former.