Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Random Binaries · Apr 21, 2011 at 01:32 PM · variabledistancecheckdestroyed

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 &lt; 2){
     dialon1 = 1;
     screenText = "First Key - Take to prisoner";
 }
 else {
     dialon1 = 0;
 }

 if(SecondDist &lt; 2){
     isNear = 1;
     screenText = "Second Key - Take to prisoner";
 }
 else{
     isNear = 0;
 }
 if(kEyDist1 &lt; 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&gt;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; }/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

check if an always changing int variable suddenly stop changing 1 Answer

check if an always increasing variable has stop increasing 2 Answers

Animation doesn't play when distant check is triggered 1 Answer

Check distance between many objects 3 Answers

Check Between Enemy List 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges