- Home /
Teleport script: Object Reference not set to an instance of an Object
When I play, I get error message:
NullReferenceException: Object reference not set to an instance of an object Teleporter.Start () (at Assets/UdacityVR/Scripts/Extras/Teleporter.cs:27)
In the hierarchy, I have a game object called 'Player', with a child Camera called 'Camera". The GVRReticlePointer is then a child of the camera.
I have the Teleporter prefab in the hirearchy too, with this teleport script attached to game object called 'Teleport', and then each of the 4 waypoints as child objects.
Line 27 is "cameraRig = Camera.main.transform.parent.gameObject;"
What is the error with the reference here?
The console goes on to detail the same error with other lines of script, all it seems trying to reference something incorrectly. Here's the script, and any help would be greatly appreciated...
public class Teleporter : MonoBehaviour { [Tooltip ("How tall is the player, in meters?")] public float height = 1.75f; [Tooltip ("How fast to move to new location?")] public float speed = 10.0f;
private Transform[] waypoints;
// Cached transforms for all waypoints
private int currentWaypointIndex = -1;
// Which waypoint is active?
private bool shouldMoveToWaypoint = false;
// Only move to the first waypoint after the player pressed the cardboard button (or touched the screen)
private GameObject cameraRig;
// Reference to the camera rig representing the player
void Start ()
{
// Assigne the camera rig reference
cameraRig = Camera.main.transform.parent.gameObject;
// Initialize the waypoints
waypoints = new Transform[transform.childCount];
for (int i = 0; i < transform.childCount; i++) {
waypoints [i] = transform.GetChild (i);
}
}
void Update ()
{
// If the player pressed the cardboard button (or touched the screen), then go to the next waypoint
if (Input.GetMouseButtonDown (0)) {
if (!shouldMoveToWaypoint) {
shouldMoveToWaypoint = true;
}
currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
}
// Smoothly move the player towards the active waypoint
if (shouldMoveToWaypoint) {
Vector3 destPos = waypoints [currentWaypointIndex].transform.position + Vector3.up * height;
cameraRig.transform.position = Vector3.Lerp (cameraRig.transform.position, destPos, Time.deltaTime * speed);
}
}
}