The question is answered, right answer was accepted
Unity 5 Input.mousePosition NullReferenceException
Hello, I used this code in Unity 4 to rotate my player to the mouse position with a top view camera. But when i updated to Unity 5 it stopped working leaving me with a NullReferenceException error both in Javascript and C#
What would have to be modified so it works in Unity 5? Sorry for my bad English
var speed = 4.0;
function Update () {
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
And here's the C# code
using UnityEngine;
using System.Collections;
public class LookAtMouse : MonoBehaviour
{
// speed
public float speed;
void FixedUpdate()
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
When you get an error you also get the line of the script that's throwing that error. Always share the line info.
The only thing that can be null there is Camera.main. This means that the camera doesn't have the "$$anonymous$$ainCamera" tag.
Follow this Question
Related Questions
IOS BUILD errors 1 Answer
Unable to restore transactions in iOS 9.2.1 using openIAB plugin. 0 Answers
Online Maps on IOs 0 Answers
Unity can not access StreamingAssets on IOS 1 Answer
Unity 5 Each Platform Support 1 Answer