- Home /
script does not work when you open the Unity
I have a script to capture the height, width and distance from the camera. This script is associated with the Main Camera and works normally.
using UnityEngine;
public class GerenciarCamera : MonoBehaviour
{
public Camera camera1;
private static float minX;
private static float minY;
private static float maxX;
private static float maxY;
private float distanciaZ;
void Start()
{
distanciaZ = transform.position.z - camera1.transform.position.z;
minX = camera1.ScreenToWorldPoint(new Vector3(0, 0, distanciaZ)).x;
maxX = camera1.ScreenToWorldPoint(new Vector3(Screen.width, 0, distanciaZ)).x;
minY = camera1.ScreenToWorldPoint(new Vector3(0, 0, distanciaZ)).y;
maxY = camera1.ScreenToWorldPoint(new Vector3(0, Screen.height, distanciaZ)).y;
}
public static float MinX()
{
return minX;
}
public static float MinY()
{
return minY;
}
public static float MaxX()
{
return maxX;
}
public static float MaxY()
{
return maxY;
}
}
By reopen the project in Unity, it seems that the script stops working. To make it work, I remove it from the Main Camera, and readd again. This way works. What could be wrong? Bug?
Thank's
Answer by KaushikRahul · Apr 16, 2016 at 07:04 AM
I have a question here.
Why do you want to calculate the distance and everything of the MainCamera from its own?? Its doesn't makes any sense to me?
See what you are doing : line 14, distanciaZ = transform.position.z - camera1.transform.position.z;
, Why? its like you are subtracting 2 from 2 and asking why it is returning 0.
if it is the values that you need then you would have used transform.position
or else if you want the distance of the camera from a particular transform, then attach this script to that GameObject.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074926im_/https://answers.unity.com/themes/thub/images/avi.jpg)