- Home /
instantiate at mouse problems
hello this is my script:
var newObject : Transform;
var mousePositionInWorld : Vector3;
function Update () {
mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePositionInWorld.y = 0;
if (Input.GetButtonDown("Fire1"))
Instantiate(newObject, mousePositionInWorld, Quaternion.identity);
}
i keep getting the error:
NullReferenceException UnityEngine.Camera.ScreenToWorldPoint (Vector3 position) (at E:/BuildAgent/work/71ca6fec1b41cc30/Runtime/Export/Generated/Graphics.cs:590) Create.Update () (at Assets/Scripts/Create.js:12)
What im trying to do is instantiate an object where the mouse is but i cant seem to get it to work. any help appreciated. thanks.
Call your assignment functions for mousePositionInWorld inside your if loop and see what that does.
and by the way there are no error until i click to instantiate then the NullReferenceException error comes up and nothing is instantiated anywhere
Have you dragged an object into "newObject" in the inspector? This error sounds like the object to be instantiated does not exist/ is not defined...