Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Orbit 2D camera around circle but keep lookat towards z axis 1 Answer

How to change a mesh using a script? 1 Answer

Sounds triggering too fast 4 Answers

GetComponent().enabled = true; 1 Answer

Toggle Control between multiple turrets 0 Answers


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