Я получаю следующее сообщение об ошибке:GetComponent: NullReferenceException
NullReferenceException: Object reference not set to an instance of an object
при попытке выбрать объект на этой линии:
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
Полный код:
using UnityEngine;
using System.Collections;
public class pickupobject :
MonoBehaviour {
GameObject mainCamera;
public float distance;
GameObject carryObject;
bool carrying;
void start() {
mainCamera = GameObject.FindWithTag("MainCamera");
}
void pickup()
{
if (Input.GetKeyDown(KeyCode.F))
{
int x = Screen.width/2;
int y = Screen.height/2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
// Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
pickupcube p = hit.collider.GetComponent<pickupcube>();
if (p != null)
{
carrying = true;
carryObject = p.gameObject;
}
}
}
}
void carry(GameObject o)
{
o.GetComponent<Rigidbody>().isKinematic = true;
o.transform.position = mainCamera.transform.position + mainCamera.transform.forward * distance;
}
// Update is called once per frame
void Update()
{
if (carrying)
{
carry(carryObject);
}
else
{
pickup();
}
}
}
Пожалуйста, помогите мне за это, спасибо заранее –
Возможный дубликат [Что такое NullReferenceException и как это исправить это?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –