Quick answer: CS0029 means the value you are assigning has one type and the variable, property or return type receiving it has another, and C# has no automatic conversion between the two. The message names both: 'A' to 'B' reads as “you have an A, this spot needs a B”. Change what you assign (take .gameObject, call GetComponent<T>(), ToString() or int.TryParse) or change the declared type. If the message ends with “An explicit conversion exists”, it is CS0266 and a cast such as (int) is allowed, though another call is often the better fix.
Unity recompiles, the Console shows a red line starting “Cannot implicitly convert type”, and Play mode is locked. The compiler is telling you that the two sides of an assignment do not match and that it will not guess how to turn one into the other. The message always contains two type names, and reading them correctly is most of the fix. This guide shows how to read them, separates CS0029 from its siblings CS0266 and CS0664, and then goes through the Unity cases that produce it: numbers, GameObjects and components, void results, strings, arrays and lists, and the 2D and TextMeshPro lookalike types.
- The exact error
- How to read the two type names
- CS0029, CS0266 and CS0664 compared
- The Unity cases, by the types in the message
- Fix 1: numbers and booleans
- Fix 2: GameObject, Transform and components
- Fix 3: void results and strings
- Fix 4: arrays, lists and vector types
- When a cast is the right fix, and how to confirm it
- What Bugnet sees of a type mismatch
- Related guides
- Frequently asked questions
The exact error
Assets/Scripts/Player.cs(18,29): error CS0029: Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.GameObject'
Assets/Scripts/Player.cs(24,22): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
Assets/Scripts/Player.cs(9,26): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
Each line gives the file, the line and column in brackets, the error code and the message. Double-click the entry in the Console to open the file at that line. These are compile errors: they appear when scripts recompile in the editor and again when a player build compiles. Nothing runs until they are fixed, and no player ever sees them.
How to read the two type names
The first type is what you have: the type of the expression on the right of the =, after return, or inside the brackets of an if. The second type is what that position needs: the declared type of the variable, field or property on the left, the method’s return type, or bool for a condition.
GameObject target = other.transform;
// needs has
// 'UnityEngine.GameObject' 'UnityEngine.Transform'
// error CS0029: Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.GameObject'
Some details of how the compiler prints types help here:
- Unity and package types are printed with their namespace:
UnityEngine.GameObject,UnityEngine.UI.Text,TMPro.TextMeshProUGUI. Your own classes usually have no namespace and appear bare, asEnemy. - Built-in types appear as keywords:
int,float,double,string,bool. []after a name means an array, so'Enemy[]'is many enemies and'Enemy'is one.'void'as the first type means the method on the right returns nothing at all.'method group'means you wrote a method’s name without calling it.
If both names are identical and each is followed by an assembly name or file path in square brackets, the project contains two different classes with the same name. That is a duplicate-type problem, covered in CS0101 and CS0111.
CS0029, CS0266 and CS0664 compared
| Code | Message ends with | What it tells you | Is a cast allowed? |
|---|---|---|---|
| CS0029 | Cannot implicitly convert type 'A' to 'B' | No conversion from A to B exists, automatic or by cast. | No. (B)value gives CS0030, Cannot convert type 'A' to 'B'. |
| CS0266 | An explicit conversion exists (are you missing a cast?) | A conversion exists, and it can lose data or fail at runtime, so you must ask for it. | Yes, with the caveats below. |
| CS0664 | use an 'F' suffix to create a literal of this type | You wrote a number with a decimal point, which is a double, where a float is needed. | Add f to the number. |
| CS0428 | Did you intend to invoke the method? | You assigned a method name without the (). | Add the parentheses. |
The same mismatch in a method call is reported as a different code, CS1503, with the wording Argument 1: cannot convert from 'A' to 'B'. The types read the same way, and the Unity cases are in the CS1503 guide.
The Unity cases, by the types in the message
Find the row whose pair of types matches your Console line.
| Line that fails | Types in the message | Fix |
|---|---|---|
int damage = baseDamage * 1.5f; | 'float' to 'int' (CS0266) | Mathf.RoundToInt(...) or (int) |
float speed = 2.5; | CS0664, double literal | 2.5f |
if (health = 0) | 'int' to 'bool' | == |
GameObject target = other.transform; | 'UnityEngine.Transform' to 'UnityEngine.GameObject' | other.gameObject |
Rigidbody rb = gameObject; | 'UnityEngine.GameObject' to 'UnityEngine.Rigidbody' | GetComponent<Rigidbody>() |
Rigidbody rb = GetComponent(typeof(Rigidbody)); | 'UnityEngine.Component' to 'UnityEngine.Rigidbody' (CS0266) | GetComponent<Rigidbody>() |
GameObject prefab = Resources.Load("Enemy"); | 'UnityEngine.Object' to 'UnityEngine.GameObject' (CS0266) | Resources.Load<GameObject>("Enemy") |
Rigidbody bullet = Instantiate(bulletPrefab); | 'UnityEngine.GameObject' to 'UnityEngine.Rigidbody' | Type the prefab field as Rigidbody, or call GetComponent on the clone |
Coroutine fade = Fade(); | 'System.Collections.IEnumerator' to 'UnityEngine.Coroutine' | StartCoroutine(Fade()) |
int total = AddScore(10); | 'void' to 'int' | The method returns nothing; give it a return type or stop assigning it |
scoreText.text = score; | 'int' to 'string' | score.ToString() |
int lives = livesInput.text; | 'string' to 'int' | int.TryParse |
Text label = GetComponent<TextMeshProUGUI>(); | 'TMPro.TextMeshProUGUI' to 'UnityEngine.UI.Text' | Declare the variable as TMP_Text |
Collider hit = Physics2D.OverlapCircle(p, 1f); | 'UnityEngine.Collider2D' to 'UnityEngine.Collider' | Declare it as Collider2D |
List<Enemy> all = FindObjectsByType<Enemy>(...); | 'Enemy[]' to 'System.Collections.Generic.List<Enemy>' | Declare an Enemy[], or build a list from the array |
Vector3Int cell = transform.position; | 'UnityEngine.Vector3' to 'UnityEngine.Vector3Int' | Vector3Int.FloorToInt(...) |
transform.rotation = new Vector3(0f, 90f, 0f); | 'UnityEngine.Vector3' to 'UnityEngine.Quaternion' | Quaternion.Euler(0f, 90f, 0f) |
If your pair is not in the table, hover the right-hand expression in your code editor. The tooltip shows its type, which is the first name in the message. Then look at the declaration of the thing on the left for the second.
Fix 1: numbers and booleans
// Broken
int damage = baseDamage * 1.5f; // CS0266: 'float' to 'int'
int index = Random.Range(0f, 3f); // CS0266: 'float' to 'int'
float speed = 2.5; // CS0664
float root = System.Math.Sqrt(0.25); // CS0266: 'double' to 'float'
bool isDead = health; // CS0029: 'int' to 'bool'
if (health = 0) { Die(); } // CS0029: 'int' to 'bool'
// Fixed
int damage = Mathf.RoundToInt(baseDamage * 1.5f);
int index = Random.Range(0, 3); // int overload: returns 0, 1 or 2
float speed = 2.5f;
float root = Mathf.Sqrt(0.25f);
bool isDead = health <= 0;
if (health == 0) { Die(); }
C# converts numbers automatically only in the widening direction: int to float, float to double. Going the other way drops information, so it needs a decision from you. (int)2.9f truncates to 2, and (int)-2.9f gives -2. If you want rounding, use Mathf.RoundToInt, Mathf.FloorToInt or Mathf.CeilToInt. Note that Mathf.Round returns a float, so assigning it to an int gives the same error.
An int multiplied by a float is a float, and any number written with a decimal point and no suffix is a double. That is why 0.5 * speed does not fit in a float variable and 0.5f * speed does. Prefer Mathf over System.Math in gameplay code, because Mathf takes and returns float.
C# has no conversion between numbers and bool in either direction. A condition must be a comparison. The if (health = 0) case is a typo for ==: a single = assigns, and the result of the assignment is an int.
The direction that needs no cast has its own trap. float ratio = hits / shots; compiles when both are int, and the division is done in whole numbers before the result is converted, so 3 / 4 is 0. Write (float)hits / shots.
Fix 2: GameObject, Transform and components
// Broken
GameObject target = other.transform; // CS0029: Transform to GameObject
Transform spawn = spawnPointObject; // CS0029: GameObject to Transform
Rigidbody rb = gameObject; // CS0029: GameObject to Rigidbody
Rigidbody body = GetComponent(typeof(Rigidbody)); // CS0266: Component to Rigidbody
GameObject prefab = Resources.Load("Enemy"); // CS0266: Object to GameObject
// Fixed
GameObject target = other.gameObject;
Transform spawn = spawnPointObject.transform;
Rigidbody rb = GetComponent<Rigidbody>();
Rigidbody body = GetComponent<Rigidbody>();
GameObject prefab = Resources.Load<GameObject>("Enemy");
A GameObject is the container. Transform, Rigidbody and your own scripts are components attached to it. None of them is a kind of the other, so no cast exists and the message is plain CS0029. Move between them with members: every component has .gameObject and .transform, and every GameObject and component has GetComponent<T>().
The CS0266 versions come from the older, non-generic calls. GetComponent(typeof(Rigidbody)) returns the base type Component, and Resources.Load("Enemy") returns UnityEngine.Object. A cast would compile, but the generic form returns the right type and is shorter.
Instantiate returns the type you pass in
[SerializeField] private GameObject bulletPrefab;
// Broken: cloning a GameObject gives back a GameObject
Rigidbody bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
// Fixed, option A: ask the clone for its component
GameObject clone = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody bullet = clone.GetComponent<Rigidbody>();
// Fixed, option B: declare the prefab field as the component you want back
[SerializeField] private Rigidbody bulletPrefab;
Rigidbody bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Cloning a component clones its whole GameObject and hands you the matching component on the copy. With option B, drag the prefab onto the field again in the Inspector, because the field’s type changed. If the prefab variable is declared as Object, the result is an Object too and you get the CS0266 form. Old tutorials write (GameObject)Instantiate(prefab) or Instantiate(prefab) as GameObject for that reason; with a field typed GameObject the cast is not needed.
2D, 3D and TextMeshPro lookalikes
// Broken
Collider hit = Physics2D.OverlapCircle(transform.position, 1f);
RaycastHit ground = Physics2D.Raycast(transform.position, Vector2.down);
Text label = GetComponent<TextMeshProUGUI>();
// Fixed
Collider2D hit = Physics2D.OverlapCircle(transform.position, 1f);
RaycastHit2D ground = Physics2D.Raycast(transform.position, Vector2.down);
TMP_Text label = GetComponent<TMP_Text>(); // needs: using TMPro;
The 2D physics types (Collider2D, Rigidbody2D, RaycastHit2D) are separate classes from the 3D ones, with no conversion between them. UnityEngine.UI.Text and TextMeshPro’s text components are unrelated in the same way. Do not look for a cast. Select the object, read which component it really has in the Inspector, and declare the variable as that type. TMP_Text is the base class of both TextMeshProUGUI (UI) and TextMeshPro (world space), so it accepts either.
Fix 3: void results and strings
// Broken
Vector3 dir = offset.Normalize(); // CS0029: 'void' to 'UnityEngine.Vector3'
int total = AddScore(10); // CS0029: 'void' to 'int'
scoreText.text = score; // CS0029: 'int' to 'string'
int lives = livesInput.text; // CS0029: 'string' to 'int'
// Fixed
Vector3 dir = offset.normalized;
AddScore(10);
scoreText.text = score.ToString();
if (int.TryParse(livesInput.text, out int lives))
{
StartRun(lives);
}
'void' as the first type means the method does its work in place and returns nothing. Vector3.Normalize() changes the vector it is called on, while the normalized property returns a new one. List.Sort(), List.Reverse() and transform.Translate() behave the same way: call them on their own line. If the method is yours and should produce a value, change void to the value’s type and add a return statement.
Text and numbers never convert by themselves. Number to text is ToString() or string interpolation ($"Score: {score}"). Text to number is parsing, and it can fail: int.Parse throws on "", "abc" and "3.5", which is the subject of FormatException: Input string was not in a correct format. int.TryParse and float.TryParse return false where Parse would throw.
Fix 4: arrays, lists and vector types
// Broken
List<Enemy> enemies = FindObjectsByType<Enemy>(FindObjectsSortMode.None);
GameObject boss = GameObject.FindGameObjectsWithTag("Boss");
Vector3Int cell = transform.position;
transform.rotation = new Vector3(0f, 90f, 0f);
// Fixed
Enemy[] enemies = FindObjectsByType<Enemy>(FindObjectsSortMode.None);
List<Enemy> enemyList = new List<Enemy>(enemies); // only if you need to add or remove
GameObject boss = GameObject.FindWithTag("Boss");
Vector3Int cell = Vector3Int.FloorToInt(transform.position);
transform.rotation = Quaternion.Euler(0f, 90f, 0f);
Unity’s “find many” calls return arrays: FindObjectsByType, GameObject.FindGameObjectsWithTag, GetComponentsInChildren, Physics.OverlapSphere. An array is not a List, and an array of one type is not a single object of that type. Either declare the variable as an array, copy it into a list with new List<T>(array), or call the singular method when you want one object.
Among vector types, the conversions that exist are the ones that cannot lose anything surprising: Vector3Int to Vector3, and Vector2 to and from Vector3. Vector3 to Vector3Int needs a rounding rule, so you choose one of Vector3Int.FloorToInt, RoundToInt or CeilToInt. For a Tilemap, tilemap.WorldToCell(transform.position) returns the cell directly. A rotation is a Quaternion; build one from angles with Quaternion.Euler, or assign the angles to transform.eulerAngles.
When a cast is the right fix, and how to confirm it
Only CS0266 offers a cast, and “exists” does not mean “safe”. For numbers the cast always succeeds and may discard part of the value. For objects the cast is checked when the line runs:
Collider col = GetComponent<Collider>();
BoxCollider box = (BoxCollider)col; // compiles; throws InvalidCastException if col is a SphereCollider
// Safer: test and convert in one step
if (col is BoxCollider boxCollider)
{
Debug.Log(boxCollider.size);
}
The as operator returns null where the cast would throw, which trades an InvalidCastException for a NullReferenceException a few lines later unless you check the result. One conversion is never reported at compile time: foreach (GameObject child in transform) compiles, because foreach inserts a cast for you, and then throws at runtime because the children of a Transform are Transforms. See InvalidCastException: Specified cast is not valid for that family.
To confirm the fix, save the file, return to Unity and wait for the recompile. Compile errors cannot be removed with Clear, so a Console that stays empty after clearing is a real pass. If you fixed the error with a cast, GetComponent or a parse, enter Play mode and exercise that line, since those are the fixes that can still fail when the game runs.
What Bugnet sees of a type mismatch
Nothing, for the compile error itself. CS0029 and CS0266 stop the build on your machine, so no player build exists and there is nothing to report. What does reach Bugnet is the runtime form of the same mistake, after a quick fix made the compiler quiet: an InvalidCastException from a cast that was wrong for one prefab, a NullReferenceException from an as or a GetComponent<T>() that returned nothing, a FormatException from parsing text a player typed. The Bugnet Unity SDK captures logged errors and exceptions through Application.logMessageReceived and files each with its stack trace, recent log lines, game version, platform and OS. Reports with the same title stack into one entry with an occurrence count.
The limit is that the report names the line that threw, not the assignment that should have been written differently. An InvalidCastException message does not say which two types were involved, so if a cast is risky, log the object’s name and type before it and that line travels with the report. The bug reports guide shows what a filed report contains.
Related guides
- How to Fix “error CS1503: Argument 1: cannot convert from” in Unity
- How to Fix “InvalidCastException: Specified cast is not valid” in Unity
- How to Fix “FormatException: Input string was not in a correct format” in Unity
- How to Fix “error CS1061: does not contain a definition for” in Unity
- How to Fix “error CS0103: The name does not exist in the current context” in Unity
Frequently asked questions
What does error CS0029 mean in Unity?
The expression on the right of an assignment, return or condition has one type and the place receiving it needs another, and C# has no conversion between them. The first type in the message is what you have and the second is what is needed. Change the expression or change the declared type.
What is the difference between CS0029 and CS0266?
CS0029 means no conversion exists at all, so a cast will not compile either. CS0266 adds the words An explicit conversion exists, which means a cast is allowed but may lose data or fail at runtime.
How do I fix cannot implicitly convert type float to int in Unity?
Decide how the fraction should be handled. Use Mathf.RoundToInt, Mathf.FloorToInt or Mathf.CeilToInt for a rounding rule, or write (int) in front of the expression to truncate toward zero.
Why can I not convert a GameObject to a Transform or a Rigidbody?
A GameObject is a container and Transform, Rigidbody and scripts are components attached to it, so none is a kind of the other and no cast exists. Use the transform property for the Transform and GetComponent with the component type for anything else. From a component, the gameObject property gives you the container.
Why does Unity say a literal of type double cannot be implicitly converted to type float?
That is error CS0664. A number written with a decimal point and no suffix is a double in C#, and Unity fields such as speeds and positions are floats. Add the f suffix, as in 2.5f.
Read the two type names before changing anything: the first is what you have, the second is what the line needs. Reach for a cast only when the message says an explicit conversion exists, and test that line in Play mode afterwards.