Hi all can anyone help me with this matter please? I'm getting a NullReferenceException error on line 19 of this script and I really want to find the solution to this because it is really bugging me. This problem stops Unity from finding my second key and showing a dialogue box with a message. Help much appreciated.
var dialon = 0;
var isTrue = 0;
var isNear = 0;
var putdown = 0;
var putdown1 = 0;
var screenText : String;
var wall = 0;
var Key_2_Npc = 0;
function Update() {
var kEy = GameObject.Find("Key");
var Sec_KEY = GameObject.Find("Second Key");
var Pc = GameObject.Find("PC");
var Npc = GameObject.Find("NPC");
var invis_Wall = GameObject.Find("Mesh Collider");
var NpcDist = Vector3.Distance(Pc.transform.position, Npc.transform.position);
var kEyDist = Vector3.Distance(Pc.transform.position, kEy.transform.position);
var SecondDist = Vector3.Distance(Pc.transform.position, Sec_KEY.transform.position);
var kEyDist1 = Vector3.Distance(kEy.transform.position, Npc.transform.position);
if(NpcDist < 2){
if(!kEy.transform.IsChildOf(Pc.transform)){
dialon = 1;
screenText = "Break me out of here and I might help you to find one
of the Shadow Thieves safehouses. Three keys
are needed to open these locks.
The first one is over there in Hunter's Alley. Be
careful though as the key might be guarded.";
}
}
else {
dialon = 0;
}
if(kEyDist < 2){
isTrue = 1;
screenText = "First Key - Take to prisoner";
}
else {
isTrue = 0;
}
if(SecondDist < 2){
isNear = 1;
screenText = "Second Key - Take to prisoner";
}
else{
isNear = 0;
}
if(kEyDist1 < 2){
screenText = "HOORAY the first key. Now to find that second
key it's somewhere hidden
on Jembril Street, I think.";
ShowChat();
gameObject.Find("Mesh Collider").active = false;
kEy.active = false;
//Destroy(kEyDist);
dialon1 = 0;
putdown = 0;
}
if(Input.GetKeyDown("m")){
putdown = 1;
kEy.transform.parent = Pc.transform;
kEy.transform.localPosition = Vector3.forward * 1.5;
}
else {
if(Input.GetKeyDown("n")){
putdown = 0;
kEy.transform.parent = null;
}
}
if(Input.GetKeyDown("j")){
putdown1 = 1;
Sec_KEY.transform.parent = Pc.transform;
Sec_KEY.transform.localPosition = Vector3.forward * 1.5;
}
else {
if(Input.GetKeyDown("k")){
putdown1 = 0;
Sec_KEY.transform.parent = null;
}
}
var fwd = Pc.transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(Pc.transform.position, fwd * 1, Color.green);
if(Physics.Raycast(Pc.transform.position, fwd, hit, 1)){
if(hit.collider.gameObject.name == "Mesh Collider"){
Debug.Log("Invisiable Wall");
Debug.Log(hit.distance);
screenText = "Oh on a invisiable wall, who or
what placed this thing here.
Might as well as give the first key
to the prisoner to get rid of this.";
ShowWall();
}
}
}
function OnGUI () {
if (dialon>0){
var dialRect : Rect = Rect ((Screen.width/2)-150, (Screen.height/2)-200, 300, 100);
GUI.Box(dialRect , screenText);
}
if (isTrue>0){
var dialRect1 : Rect = Rect ((Screen.width/2)-150, (Screen.height/2)-200, 300, 100);
GUI.Box(dialRect1 , screenText);
}
if (isNear>0){
var dialRect2 : Rect = Rect ((Screen.width/2)-150, (Screen.height/2)-200, 300, 100);
GUI.Box(dialRect2 , screenText);
}
if (wall>0){
var dialRect100 : Rect = Rect ((Screen.width/2)-150, (Screen.height/2)-200, 300, 100);
GUI.Box(dialRect100 , screenText);
}
if (Key_2_Npc>0) {
var dialRect3 : Rect = Rect ((Screen.width/2)-150, (Screen.height/2)-200, 300, 100);
GUI.Box(dialRect3, screenText);
}
}
function ShowWall() {
wall = 1;
yield WaitForSeconds(2);
wall = 0;
}
function ShowChat() {
Key_2_Npc = 1;
yield WaitForSeconds(10);
Key_2_Npc = 0;
}