{"id":263,"date":"2020-07-10T17:30:56","date_gmt":"2020-07-10T17:30:56","guid":{"rendered":"http:\/\/plbm.com\/?p=263"},"modified":"2020-07-10T21:57:51","modified_gmt":"2020-07-10T21:57:51","slug":"how-to-fix-cannot-implicitly-convert-type-c-errors","status":"publish","type":"post","link":"http:\/\/plbm.com\/?p=263","title":{"rendered":"How to fix &#8220;Cannot implicitly convert type&#8221; C# errors"},"content":{"rendered":"\n<p>One common error when using C# is attempting to assign one type of object to another type, and the error can be reported as<\/p>\n\n\n\n<ul><li>Cannot implicitly convert type &#8216;Xxxx&#8217; to type &#8216;Yyyy&#8217;<\/li><li>Cannot convert from &#8216;Xxxx&#8217; to &#8216;Yyyy&#8217;<\/li><li>Argument Zzzz: cannot convert from &#8216;Xxxx&#8217; to &#8216;Yyyy&#8217;<\/li><\/ul>\n\n\n\n<p>The error means you have an apple, and you&#8217;re trying to put it in the orange slot, or some other inappropriate assignment.<\/p>\n\n\n\n<p>Like &#8220;Null Reference&#8221; and &#8220;Index Out Of Bounds&#8221; exceptions, the &#8220;Cannot Convert&#8221; family of exceptions are extremely common and generally simple-to-fix errors, but you need to understand what you are doing.<\/p>\n\n\n\n<p>Fortunately the compiler gives you a great start and tells you exactly which line the error is on, so double-click that error in the console and go look at your code.<\/p>\n\n\n\n<p>Sometimes you can add something to facilitate making what you want to happen, and this is called casting.<\/p>\n\n\n\n<p>But far more often you simply are trying something that can&#8217;t be done, for instance assigning a float into a Vector3. A Vector3 is three (3) separate floats named x, y, z, so in that case you can only assign your float into one of those floats.<\/p>\n\n\n\n<p>To reason about what you are doing, you need to understand two things:<\/p>\n\n\n\n<ul><li>What are you assigning (the part to the right of the equal sign)<\/li><li>What are you assigning it to (the part to the left of the equal sign)<\/li><\/ul>\n\n\n\n<p>Stop and ask yourself, what type of object is on the right? Is that a float? Is it a GameObject? An int? A Vector3? A Quaternion? Figure that out first. Don&#8217;t go any further.<\/p>\n\n\n\n<p>You absolutely must understand what you are trying to assign in order to actually function as a software engineer. It&#8217;s not optional so start now.<\/p>\n\n\n\n<p>Then go to the left side and say &#8220;Where I am putting this, what is this item?&#8221; If it isn&#8217;t the exact same type of object, it probably won&#8217;t work.<\/p>\n\n\n\n<p>Now you have some clues to begin investigating what you are trying to accomplish. Start with asking yourself, &#8220;What is the purpose of this line of code?&#8221; If you can articulate clearly what you&#8217;re trying to do, that implies you know the source and the destination data types.<\/p>\n\n\n\n<p>And finally if you get stuck, start googling the error. That&#8217;s almost always going to give you some insight.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One common error when using C# is attempting to assign one type of object to another type, and the error can be reported as Cannot implicitly convert type &#8216;Xxxx&#8217; to type &#8216;Yyyy&#8217; Cannot convert from &#8216;Xxxx&#8217; to &#8216;Yyyy&#8217; Argument Zzzz: cannot convert from &#8216;Xxxx&#8217; to &#8216;Yyyy&#8217; The error means you have an apple, and you&#8217;re &hellip; <a href=\"http:\/\/plbm.com\/?p=263\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to fix &#8220;Cannot implicitly convert type&#8221; C# errors<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/plbm.com\/index.php?rest_route=\/wp\/v2\/posts\/263"}],"collection":[{"href":"http:\/\/plbm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/plbm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/plbm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/plbm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=263"}],"version-history":[{"count":10,"href":"http:\/\/plbm.com\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":275,"href":"http:\/\/plbm.com\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions\/275"}],"wp:attachment":[{"href":"http:\/\/plbm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/plbm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/plbm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}