- Home /
destroyed gameobject breaks variable detecting distance
Can anyone help me with this problem please? I'm getting a NullReferenceException error and I know why but I don't know how to solve this. The problem surrounds the variable used to detect distance between two objects called kEyDist. Within the if statement that checks to see whether the distance of the key is close to NPC it destroys the key. Because the key is destroyed the variable kEyDist is then missing the Key gameobject because it was destroyed. This then has a knock on effect of not detecting the distance between the character and the second key. If anyone can come up with a solution to this it would be much appreciated.
var dialon = 0; var dialon1 = 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 \n of the Shadow Thieves safehouses. Three keys \n are needed to open these locks. \n The first one is over there in Hunter's Alley. Be \n careful though as the key might be guarded."; } } else { dialon = 0; }
if(kEyDist < 2){
dialon1 = 1;
screenText = "First Key - Take to prisoner";
}
else {
dialon1 = 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 \n key it's somewhere hidden \n on Jembril Street, I think.";
ShowChat();
Destroy(gameObject.Find("Mesh Collider"));
Destroy(kEy);
//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 \n what placed this thing here. \n Might as well as give the first key \n 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 (dialon1>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; }
/function ShowDialogue(){ dialon2 = 1; yield WaitForSeconds(2); dialon2 = 0; }/
Answer by burgunfaust · Apr 21, 2011 at 01:53 PM
You could just move the key instead of destroying it outright. You only have 2 keys, it's not like there are 200 keys. Just move it off camera.