🤔 Unity, huh, how?

🤔

CS0266

Cannot implicitly convert type 'Foo' to 'Bar'. An explicit conversion exists (are you missing a cast?)

Variables can only be assigned to one another if they follow some basic rules:

Resolution

Often this error is shown when there's a misunderstanding of the types involved.

Floats and doubles

3.2 is a double, 3.2f is a float. There is no implicit conversion from doubles to floats as there could be data loss converting from larger to smaller data.
If your code looks like:

public float gravity = -9.81; // Should be: -9.81f
position.x += 0.5; // Should be: 0.5f

Then you've likely failed to append the f to indicate the float type.

Other types

Understand the types of the two objects involved in the error. Documentation and intellisense will help guide you to using the correct variables and syntax.

Examples:

// Should be: transform.position;
// Transforms are not positions, they have positions.
Vector3 position = gameObject.transform;
// Should be: new Vector3(0, 1, 0);
// This isn't how a Vector3 is constructed.
Vector3 position = (0, 1, 0);
// Should be: GetComponentsInChildren<Example>();
// The singular version of the method has been mistaken for the plural.
Example[] data = GetComponentInChildren<Example>();

Sometimes this error can be due to a mistake made by using assignment instead of comparison:

// Should be: if (gameObject.name == "Example")
// Assignment "=" has been used instead of equality "=="
if (gameObject.name = "Example")
{
    ...
}