- Home /
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);
}
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);
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?
Do you have a camera in your scene tagged "$$anonymous$$ain Camera"?
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
Follow this Question
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