Float calculation always outputting 0 when multiplied with another float....
The following is whats not working, basically every time I multiply the local position y of the object by any value other than an integer value declared in the shown script, it results in 0 and hence the object itself is moved to the origin , I've tried using doubles and all sorts of weird solutions , although to no avail , the y position of the object is currently really small at about -0.0195 , and i,m not sure if its just because the value is so small that when put into a calculation it's just read as 0 or not. If you are wondering the vertExtent is the orthographic size of the camera in my scene and is set to 8 , so theoretically the value from this should be 1 * ... but still doesn't seem to work.
using UnityEngine;
using System.Collections;
public class ScaleLazerNodes : MonoBehaviour {
// Use this for initialization
void Start () {
gameObject.transform.localPosition = new Vector3(0, gameObject.transform.localPosition.y * (Spawner.vertExtent) / 8, 0);
}
// Update is called once per frame
void Update () {
}
}
Answer by btmedia14 · Jun 28, 2016 at 10:35 PM
The y position of -0.0195 is small and effectively, with x and z at zero, the object is close to the origin. Nevertheless, the calculation looks ok.
I assume Debug.Log for each stage of the calculation was used to display individual parts of the calculation. If so, note that Debug.Log defaults to rounding 1 decimal place, and so in this example will always display y as 0.0. Suggest using a string formatter to display more decimal places:
Debug.Log(gameObject.transform.localPosition.ToString("F4"));
The calculation here is affecting its local position , in relation to its parent , so even being as small an offset as it is, visually its at the edge of the camera (due to the scale of its parent affecting this) until i try to do any sort of calculation to it , sorry probably should have made that bit clear. Like i mentioned previously if i just do integer * gameObject.transform.localPosition.y it works fine , although as soon as i try using the camera size it doesn't work despite the value still being 1. $$anonymous$$aybe i'm just being rather dim but i really cant find a solution to this other than reworking the setup i have currently.
I am unable to make this fail ! The Vector3 generated values all seem to work fine no matter combinations of parent transforms or camera orthographic sizes. The only thing I can suggest is Debug.Log the value of Spawner.vertExtent to ensure it is always non-zero - again use string formatter to get more decimal places.
Perhaps also try changing the 8 in the calculation to 8.0f to force a float type.
If Spawner.vertExtent is defined as an Int, with a value less than 8, the division may occur as a division of integers resulting in a zero integer value before remainder of calculation. This is the only case I was able to create a zero y value.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083851im_/https://answers.unity.com/themes/thub/images/avi.jpg)