Witam.
Mam problem z unity, a mianowicie, gdy chce odpalić Symulacje/gre Pokazuje mi się błąd:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.Update () (at Assets/PlayerController.cs:37)
Oto kod:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
public Transform cameraTransform;
public float playerWalkingSpeed = 5f;
public float playerRunningSpeed = 15f;
public float jumpStrength = 5f;
public float verticalRotationLimit = 80f;
Vector3 forwardMovement;
Vector3 sidewaysMovement;
Vector3 verticalVelocity;
float verticalRotation = 0;
CharacterController cc;
void Awake()
{
cc = GetComponent<CharacterController>();
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float horizontalRotation = Input.GetAxis("Mouse X");
transform.Rotate(0, horizontalRotation, 0);
verticalRotation -= Input.GetAxis("Mouse Y");
verticalRotation = Mathf.Clamp(verticalRotation, -verticalRotationLimit, verticalRotationLimit);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 50, 0);
forwardMovement = cameraTransform.forward * Input.GetAxis("Vertical") * playerWalkingSpeed;
sidewaysMovement = cameraTransform.right * Input.GetAxis("Horizontal") * playerWalkingSpeed;
if (Input.GetKey(KeyCode.LeftShift))
{
forwardMovement = cameraTransform.forward * Input.GetAxis("Vertical") * playerRunningSpeed;
sidewaysMovement = cameraTransform.right * Input.GetAxis("Horizontal") * playerRunningSpeed;
}
verticalVelocity += Physics.gravity * Time.deltaTime;
if (Input.GetButton("Jump") && cc.isGrounded)
{
verticalVelocity = new Vector3(0, jumpStrength, 0) * Time.deltaTime;
}
Vector3 playerMovement = sidewaysMovement + verticalVelocity + forwardMovement;
cc.Move(playerMovement * Time.deltaTime);
}
}
Proszę o jak najszybszą odpowiedź.