- Home /
Transform.position problem Please help?
I have the following script. When the user clicks a button, I want the camera to move to an other position, but it doesn't work. Any help?
var cameraobj : GameObject;
var target : GameObject;
function OnMouseUp()
{
cameraobj.transform.position = target.position;
}
Answer by Eric5h5 · Oct 07, 2012 at 06:13 PM
If you want input in general, use the Input class in Update. OnMouseUp is a callback for clicking on a particular object.
But i want the user to press a button to move the camera, not just click anywhere... :(
Then add if statements so it only works when the user presses the button you want.
I changed the script to this
var cameraobj : Camera; var target : GameObject; var pressed : boolean;
function Update() { if(Input.Get$$anonymous$$ouseButtonDown(1) && pressed == true) { cameraobj.transform.position = target.position; } }
function On$$anonymous$$ouseUp() { pressed = true; }
but it continues have an error:
NullReferenceException: Object reference not set to an instance of an object Camera $$anonymous$$ove.Update () (at Assets/Camera $$anonymous$$ove.js:9)
When you double click the NullReferenceException in the console, what line does it highlight in your code?
Also - you don't need to add the pressed variable, you only need Unity's included $$anonymous$$ouseDown/$$anonymous$$ouseUp check functions.
if (Input.Get$$anonymous$$ouseButtonDown(1)) cameraobj.transform.position = target.position;
it is at line 9. if i put the if (Input.Get$$anonymous$$ouseButtonDown(1)) cameraobj.transform.position = target.position;
when i click anywhere, the camera, but again it has this error
NullReferenceException: Object reference not set to an instance of an object Camera $$anonymous$$ove.Update () (at Assets/Camera $$anonymous$$ove.js:9)
(the script is attached to the box/button, should i change this?
Your answer
