我试图在我的项目中为我的汽车制作工作钥匙,但是当我尝试引用另一个脚本时,我得到一个错误说:
NullReferenceException:未将对象引用设置为对象的实例 DrivingScript.Update()(在Assets / Car /尝试制作自定义Car / DrivingScript.js:21)
我试图查一查,但如果你能帮忙就找不到问题。
var Car : Transform;
var player : Transform;
var exitPoint : Transform;
var tentplayer : Transform;
var campfire : Transform;
var doorTriggerLeft : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera;
var isPlayerVisable : boolean;
private var invScript : Inventory;
function Start () {
invScript = GetComponent(Inventory);
}
function Update (){
if (CarCamera.enabled == false && invScript.LetDrive == true)
{
if (Input.GetKeyDown("r")&& isPlayerVisable){
//Make player invisable and still standing
player.gameObject.SetActiveRecursively(false);
player.gameObject.active = false;
// Parent player to ExitPoint
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(-1.5,2,0);
//Parent playerParent to car
exitPoint.parent = Car.transform;
exitPoint.transform.localPosition = Vector3(-0.5,2,0);
// Enable car as controllable object
GameObject.Find("CarJeep").GetComponent("Car").enabled = true;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
}
}
else
{
if (CarCamera.enabled == true)
{ if (Input.GetKeyDown("r")){
// Make Character visable again.
player.gameObject.SetActiveRecursively(true);
player.gameObject.active = true;
// PUT CAFTABLE ITEMS HERE\/ \/ \/
campfire.gameObject.SetActiveRecursively(false);
tentplayer.gameObject.SetActiveRecursively(false);
// PUT CRAFTABLE ITEMS HERE ^^^^
// Unparent Player from everything.
player.transform.parent = null;
// Parent Exit Point to Door Trigger.
exitPoint.parent = doorTriggerLeft.transform;
// Disable car as a controllable
GameObject.Find("CarJeep").GetComponent("Car").enabled = false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
}
}
}
}
function OnTriggerEnter(Player : Collider) {
isPlayerVisable = true;
}
function OnTriggerExit(Player : Collider) {
isPlayerVisable = false;
}

答案 0 :(得分:0)
看起来您没有在编辑器中设置CarCamera属性并将其保留为null,而此脚本依赖于它的设置。