How can I define a box using Camera.ScreenToWorldPoint?

Alright, so what I’m trying to do with this script is to define a box (or rather, define a range of possible x and y coordinates) using Camera.ScreenToWorldPoint in a 2D game, as follows:

#pragma strict

var mainCam : Camera;
private var randomPointBox : Vector3 = new Vector3 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 1, 0, 0)).x, mainCam.ScreenToWorldPoint (new Vector3 (Screen.height *1, 0, 0)).y, 0);
private var pointB = Vector3;
        
function (Start) 
{
	pointB = new Vector3 (Random.Range (-randomPointBox.x, randomPointBox.x), Random.Range (-randomPointBox.y, randomPointBox.y), randomPointBox.z);
	Debug.Log (pointB);
}

When I build or save the script, I get no errors, but when I attempt to drag my game object “MainCamera” into the “mainCam” variable I can’t, initially anyway. If I create a prefab of the “MainCamera” game object, I can drag it in, but when I run the game, point B prints as (0.0, 0.0, 0.0), and I get a null reference exception:

NullReferenceException: Object reference not set to an instance of an object
script…ctor () (at Assets/Scripts/Script.js:4)

which corresponds to line 4, my randomPointBox variable, and is therefore referring to “mainCam” not having an instance of the MainCamera, which makes total sense since I can only pull in the prefab of the main camera in the main Unity window.

I’ve watched a few tutorials where people use a similar technique, and just drag the main camera in from the hierarchy. What am I doing wrong?

Thanks again - I’ve only been with the Unity community for a few days and you’ve already been amazingly helpful. Hopefully soon I’ll be able to start answering questions, not just asking them.

You have a number of issues here. The script above will not compile, which is likely why you could not do the drag and drop, or it could be the initialization you were attempting on line 4. Anyway, I believe this is where you are heading:

#pragma strict
 
var mainCam : Camera;
private var randomPointBox : Vector3; 
private var pointB : Vector3;
 
function Start()  {
	randomPointBox = mainCam.ScreenToWorldPoint (Vector3 (Screen.width, Screen.height, 0));
    pointB = new Vector3 (Random.Range (-randomPointBox.x, randomPointBox.x), Random.Range (-randomPointBox.y, randomPointBox.y), randomPointBox.z);

    Debug.Log (pointB);
}

Note if you are using a perspective camera, you need to see the ‘z’ parameter of the Vector3 you pass to ScreenToWorldPoint() to a distance in front of the camera instead of 0.

Now I did not understand what you are trying to achieve but here’s two examples that might help you.

For further assistance, please specify your goal short and simple :slight_smile:

using UnityEngine;
using System.Collections;

public class WorldPoint : MonoBehaviour {

    public GameObject boxPrefab;

	// Update is called once per frame
	void Update () {
	
        if (Input.GetMouseButtonDown(0))
        {
            ScreenToWorldPointTest();
            ScreenPointToRayTest();
        }
	}

    void ScreenToWorldPointTest()
    {
        // Note that the returned value will appear to be "behind" the camera due near clipping
        // Also not very useful when using perspective projection
        Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
    }

    void ScreenPointToRayTest()
    {

        // Cast a ray will hit whatever is under your mouse up to 100.0f and Instantiate a prefab there to make sure it works right
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100.0f))
        {
            Instantiate(boxPrefab, hit.point, Quaternion.identity);
        }
    }
}