point-and-click dont work
I am developing a point and click mechanic in my game but everytime I click anywhere on the screen an error message occures: NullReferenceException: Object reference not set to an instance of an object RTSUnitmove.Update () (at Assets/Scripts/RTSUnitmove.js:8)
this only happens when i am using an rts view and not when i am using an fps view.
I hope you can help me out whith this.
var speed : float = 20.0;
var move : boolean;
var wantedPosition : Vector3;
function Update () {
if ( Input.GetMouseButtonDown ( 1 ) ) {
var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );
var hit : RaycastHit;
if ( Physics.Raycast (ray, hit) ) {
move = true;
wantedPosition = hit.point;
}
}
if ( move ) {
if ( Vector3.Distance ( transform.position, wantedPosition ) < 1.5 ) {
move = false;
}
else {
transform.LookAt ( wantedPosition );
transform.Translate ( Vector3.forward * Time.deltaTime * speed );
}
}
}
Is your RTS camera tagged as "$$anonymous$$ainCamera"? If not then Camera.main will return null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612120028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
prefab instatiating in a certain position, size and rotation? 0 Answers
UI and 3d raycast crossover solution 1 Answer
How can I spawn a game object/prefab on my mouse pointer while only hovering over a specific area? 1 Answer
Pathfinding for 2D sidescroller/platformer (for point-and-click purposes) 0 Answers
Having trouble with framework for a 2d point-and-click game 0 Answers