• 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
-1
Question by Zypherr7 · Nov 04, 2014 at 05:16 PM · c#camera

Float and Double conversion issues

I am getting the error message:

Assets/Code/Player Code/SwordAttack.cs(29,17): error CS0266: Cannot implicitly convert type double' to float'. An explicit conversion exists (are you missing a cast?)

Assets/Code/Player Code/SwordAttack.cs(30,17): error CS0266: Cannot implicitly convert type double' to float'. An explicit conversion exists (are you missing a cast?)

Assets/Code/Player Code/SwordAttack.cs(31,17): error CS0266: Cannot implicitly convert type double' to float'. An explicit conversion exists (are you missing a cast?)

I am not trying to convert anything to double anywhere in my code. I cannot see where it it trying to convert the variable. Any help is greatly appreciated.

 using UnityEngine;
 using System;
 
 [AddComponentMenu("Player/Sword Attack")]
 
 class SwordAttack : MonoBehaviour{
     //finds camera angle x and y for process
     float basecordinantx;
     float basecordinanty1;
     float basecordinanty;
     float basecordinantz;
         //gets mouse cordinants in aray
     int[] sworddirection;
     //processed mouse cordinants
     int[] swordpath;
     //counts the number in the array
     int arraynum;
 
     //particles
     public ParticleSystem SwordLine;
     
     
     void Update()
     {
                 //if left mouse is clicked
         if (Input.GetMouseButton (0))return;
         float camx = Camera.main.transform.eulerAngles.x;
         float camy = Camera.main.transform.eulerAngles.y;
         basecordinantx = Math.Sin (camy);
         basecordinantz = Math.Sin (camx);
         basecordinanty1 = Math.Cos (camx) + Math.Cos (camy);
         basecordinanty = basecordinanty1 / 2;
         Debug.Log (basecordinantx);
         Debug.Log (basecordinanty);
         Debug.Log (basecordinantz);
 
         }

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

1 Reply

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

Answer by robertbu · Nov 04, 2014 at 05:18 PM

The 'Math' is a .NET class. Math.Sin() and Math.Cos() both return doubles, and you are assigning the result to a float. The best fix is to use Unity's 'Mathf' class (note the 'f' on the end):

  basecordinantx = Mathf.Sin (camy);

Or if there is some reason you really want the 'Math' class, you can do:

  basecordinantx = (flaot)Math.Sin (camy);
Comment
Add comment · Show 3 · 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 Zypherr7 · Nov 04, 2014 at 06:21 PM 0
Share

Thanks robertbu. That fixed the issue of converting it into double. After I fixed this however, it gave me another error message:

NullReferenceException: Object reference not set to an instance of an object SwordAttack.Update () (at Assets/Code/Player Code/SwordAttack.cs:27)

It seems to think that Camera.main.transform.eulerAngles.x returns with no number. I thought it was measuring the angle of the camera. If it was, shouldn't there always be a number assigned to that?

avatar image tanoshimi · Nov 04, 2014 at 06:37 PM 0
Share

Do you have a camera in your scene tagged "$$anonymous$$ain Camera"?

avatar image Zypherr7 · Nov 04, 2014 at 06:42 PM 0
Share

Thanks tanoshimi, that is the issue. I originally was scripting to the main camera in the scene but then later just attached a camera to the player and got rid of the main camera.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

how to controll second camera (MiniMap)? 1 Answer

MonoDevelop 4.0.1 code completion doesn't work 2 Answers

Unassigned Reference Exception 4 Answers

Saving class fields in the Asset 0 Answers

Problems implementing a 3rd person static camera 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges