• 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 26, 2011 at 07:05 PM · nullreferenceexception

NullReferenceException error

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 \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){
         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 \n key it's somewhere hidden \n 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 \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 (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; }

Comment
Add comment · Show 2
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
avatar image superpig ♦♦ · Apr 26, 2011 at 07:08 PM 0
Share

Which line, exactly, is line 19?

avatar image Peter G · Apr 26, 2011 at 07:19 PM 0
Share

http://answers.unity3d.com/questions/41680/what-is-a-null-reference-exception-in-unity This might help some. $$anonymous$$y guess is that you typed in one of the names wrong.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bryan 4 · Apr 26, 2011 at 07:20 PM

var kEyDist = Vector3.Distance(Pc.transform.position, kEy.transform.position); for me that is line 19. Ensure that Pc exists and kEy exists (you use GameObject.Find() it will return null if it does not find an item with that name (case sensitive).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How can this possibly be null? 2 Answers

NullReferenceException After A Few Hours? 1 Answer

Object reference not set to an instace of an object (JS) 0 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException 3 Answers


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