• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ian-McCleary · Apr 10, 2015 at 10:26 PM · c#script errorintconvert

Error CS0266: Cannot implicitly convert type `double' to `int'. An explicit conversion exists (are you missing a cast?)

I am trying to find the area of a circle, but only to a few decimals (3.14 is all i really need). Anyway i get this error once i made the script and my research hasnt found an answer. Im fairly new to C# so i dont understand many complex functions. here is the error

error CS0266: Cannot implicitly convert type double' to int'. An explicit conversion exists (are you missing a cast?)

Here is the script

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 
 public class Calculations : MonoBehaviour {
     
     public GameObject textField_1;
     public GameObject textField_2;
     public GameObject textField_3;
     public Text Result;
     InputField t1;
     InputField t2;
     InputField t3;
 
     void Start()
     {
         t1 = textField_1.GetComponent<InputField> ();
         t2 = textField_2.GetComponent<InputField> ();
         t3 = textField_3.GetComponent<InputField> ();
     }
 
     
     public void Product() {
         int a = Convert.ToInt32(t1.text);
         int b = Convert.ToInt32(t2.text);
         int c = Convert.ToInt32 (t3.text);
         int d = a / 2;
         int e = (3.14159 * d) * (3.14159 * d); 
         Result.text = d.ToString ();
     }
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by winsjansen · Apr 10, 2015 at 10:38 PM

You are trying to convert type double to int, as your error message say it's really that simple.

I can't tell you exactly where as you didn't include the line from the console error message, but I'm pretty sure that this line is the problem as it seems like the user can decide the value of a(?)

 int d = a / 2;


You can't divide int by int and make it an int if the output will become a double, for example;

  • 10/5 = 2 (this is okay, 2 is int).

  • 10/0.1337 = 74,79 (this is not an int, this is a double so you can't make it an int).

You can solve this by obviosly change the variable to a double, or you can use MathF.RoundToInt(); if you cast the value to a float;

 int d = MathF.RoundToInt((float)(a / 2));

I hope this helps, if not let me know and I'll try to further assist your issue.

EDIT:

I overlooked this line that you got;

 int e = (3.14159 * d) * (3.14159 * d); 

You can't use this line, that makes no sense. You my friend need to read more about the different variable types to learn how this works.

I STRONGLY advise you to watch BOTH of these short clips on C#, I know the audio suck but this guy is amazing. I in fact advice you to watch pretty much the entire playlist, at least around the first 50 is mandatory stuff, good luck :)

C# Beginners Tutorial - 4 - Variables

C# Beginners Tutorial - 35 - More Variable Types

C# Beginners Tutorials Playlist

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ian-McCleary · Apr 11, 2015 at 04:49 AM 0
Share

Thank you so much man...i was hitting my head when i remembered integers are only whole numbers. That tutorial was great for beginners like me. Using a double ins$$anonymous$$d of an int worked great. I had no idea a double even existed for variables. Thanks again!!

avatar image
0

Answer by Oribow · Apr 10, 2015 at 10:44 PM

The variable int only stores numbers without a comma. Like 1; 6; 373... Double stores numbers with comma. Like 1.234; 4.976. Change the type of d and e to double.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 1 Answer

Convert string to int C# 1 Answer

Converting a string to an int 2 Answers

Convert Text to float 3 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges