CS0266 compile error, nullable autocast not working after null check

I’m trying to create an object that can have a destination, but may also not have a destination. So I created a Vector2? moveDestination field in my class.

public class Resource: MonoBehaviour
{
    // Configuration
    public float speed = 1.0f;

    // State
    private Vector2? moveDestination = null;

    public void Update() {
        if (moveDestination != null) {
            Vector2 nonNullMoveDestination = moveDestination;

            float distance = speed * Time.deltaTime;
            Vector2 newPosition = Vector2.MoveTowards(transform.position, nonNullMoveDestination, distance);
        }
    }
}

I get a CS0266 error stating that it can’t auto convert from Vector2? to Vector2. But as far as I understand, the null check should have caused an auto conversion.

There might even be a bug report for this on Unity: https://issuetracker.unity3d.com/issues/error-cs0266-when-using-implicit-operator-to-convert-nullable-struct-to-non-nullable (hard to check, as that bug report uses an attached script that I cannot download).

One solution is to do:

            Vector2 nonNullMoveDestination = (Vector2)moveDestination;

But I’m wondering what is the idiomatic C# / Unity way to deal with this situation.

(Note, my background is Kotlin, so I’ve grown rather accustomed to auto-conversion, and would love to also have this in C#/Unity.)

Hi,

In which course and lecture are you?

Why would you check the Vector2 variable for null? Unless you are receiving data from a non-C# source, the object is never null because structs are value types.

See this answer:

Hi Nina,

I’m doing Unity 2D and have finished Laser defender (section 6) and started on glitch garden (section 7). This is something that I was thinking of adding to my Laser Defender game, but then got lost in how this works in C#.

I was trying to understand how to have an optional destination Vector2 in my class. Something that could be there or not (depending on other things that happen in my game). So there would be another method (something like SetDestination) where some other part of the game could set a destination for my Resoure game object.

“Vector2” cannot be null, but I tried using “Vector2?” instead.

The default value of a Vector2 object is (0, 0, 0). I’m still not sure what you are trying to achieve and why you would check for null. What is the definition of “optional” in your scenario? What exactly is supposed to happen in your code?

I have rewritten my code to rely on a second variable. Hopefully this will make clear what I want to accomplish.

public class Resource : MonoBehaviour
{
    // Configuration
    public float speed = 1.0f;

    // State
    private Vector2 destination;

    private bool hasDestinationSet = false;

    public void Update() {
        if (hasDestinationSet) {

            float distance = speed * Time.deltaTime;
            Vector2 newPosition = Vector2.MoveTowards(transform.position, destination, distance);
            transform.position = newPosition;

            if (newPosition == destination) {
                hasDestinationSet = false;
            }
        }
    }

    // Called every time a player survives a wave.
    public void SetDestination(Vector2 newDestination) {
        this.destination = newDestination;
        this.hasDestinationSet = true;
    }

    public void OnTriggerEnter2D(Collider2D collision) {
        // reward player for collecting this resource
    }
}

I don’t want my game object to move towards (0,0,0). In other languages you can make Vector2 optional (nullable). I am hoping not to need an additional variable like hasDestinationSet.

Yep, that makes sense. Thank you.

Where does the compiler complain? As the API states, the nullable type was introduced in C# 8 but Unity usually uses an older version of the .NET Framework. See here.

Check the “Api Compatibility Level” in the Options.

If you cannot make this work with the nullable type, use a bool. That would have been my suggestion as well.


C# 8 seems to be supported as of Unity 2020.2. See here.

1 Like

Thanks, this makes sense! I’ll check out the options you provided.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms