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 Hugs Are Drugs · Aug 21, 2013 at 07:09 PM · c#nullreferenceexception

NullReferenceException for reasons I don't understand.

I don't know why, I'm tired and I'm about to rip my god damn head off. Just, just tell me why this is happening to me.

Here are the two codes where I get them.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Controls : MonoBehaviour 
 {
     Vector3 angles;
     public static Transform selected;
     float rotatospeed;
     float anglespeed;
     
     float angle;
     GameObject ghost;
     
     Vector3 position, rotato;
     public static Vector3 direction;
     
     public static int count;
     List<Vector3> positions;
     RaycastHit rayhit;
     
     public float speed;
     public GameObject prefab;
     public static int dosh;
     
     void Start () 
     {    
         rotato = Game.rotato;
         count = 0;
         positions = new List<Vector3>();
         dosh = System.Convert.ToInt32 (Data.XMLread ("requirments", "money"));
     }
     
     void Update () 
     {
         Game.rotato = rotato;
         rotato.x += Input.GetAxis ("Vertical") * speed * Time.deltaTime;
         rotato.y += Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
         rotato.x = Mathf.Clamp (rotato.x, 5, 85);
         transform.eulerAngles = rotato;
         
         angle += Input.GetAxis ("Rotate") * 45 * Time.deltaTime;
         angle = angle >= 360 || angle <= 0 ? 0 : angle;
         direction.y = Mathf.Round (angle * 5) / 5;
         direction.z = 90;
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if (ghost != null) 
         {
             Destroy (ghost);
         }
 
         if (Physics.Raycast (ray, out rayhit)) 
         {    
             if (Game.mode == "Build")
             {
                 if (rayhit.transform.name == "Ground" && (dosh - 500) >= 0)
                 {
                     position = rayhit.point;
                     position.x = Mathf.Round (position.x * 5) / 5;
                     position.y = 0;
                     position.z = Mathf.Round (position.z * 5) / 5;
                     
                     ghost = Instantiate (prefab, position, Quaternion.Euler (direction)) as GameObject;
                     if (Input.GetButtonDown ("Fire1"))
                     {
                         if (!positions.Contains (position)) 
                         {
                         count += 1;
                         dosh -= 500;
                     
                         positions.Add (position);
                         Instantiate (prefab, position, Quaternion.Euler (direction));                    
                         }
                     }
                 }
                 
                 if (Input.GetButtonDown ("Fire2")) 
                 {
                     if (rayhit.transform.name == "Tree Part") {
                         //null
                     
                         Destroy (rayhit.transform.parent.gameObject);
                     }
                 
                     if (rayhit.transform.name == "Reflector") 
                     {
         
                         count -= 1;
                         dosh += 500;
             
                         Destroy (rayhit.transform.parent.gameObject);
                         positions.Remove (rayhit.transform.parent.position);
                     }
                 }
             }
         }
         
         if (Game.mode == "Edit") 
         {
             if (selected != null) 
             {
                 direction.y = angle;
                 selected.eulerAngles = direction;
                 direction.y = Mathf.Round (angle * 5) / 5;
                 
                 
                 
             }
             
             if (Input.GetButtonDown ("Fire1") && rayhit.transform.name == "Reflector") 
             {
                 selected = rayhit.transform.parent;
                 angle = selected.eulerAngles.y;
             }
         }
     }
 }

and

 using UnityEngine;
 using System.Collections;
 
 public class Game : MonoBehaviour 
 {
     bool swap;
     public static Vector3 rotato;
     string click, goals;
     public static string mode;
     public static bool pause;
     public GUISkin skin;
     
     void Start () {
         mode = "Build";
     }
     
     void Update () 
     {
         goals = Data.XMLread ("goals", "goalA") + "\n\n" +  Data.XMLread ("goals", "goalB")+ "\n\n" + Data.XMLread ("goals", "goalC");
     }
     
     void OnGUI () {
         GUI.skin = skin;
         if (pause == false && Application.loadedLevelName != "Menu") 
         {
             GUI.Label (new Rect (0, 0, 200, 40), "Challenge " + Application.loadedLevelName);
             GUI.Label (new Rect (0, 20, 200, 40), "Budget: $" + Data.XMLread ("requirments", "money"));
             GUI.Label (new Rect (0, 40, 200, 40), "Budget Remaining: $" + Controls.dosh);
             
             GUI.Label (new Rect (0, Screen.height - 80, 200, 40), mode + " " + "Mode");
             
             if (GUI.Button (new Rect (Screen.width - 600, Screen.height - 40, 200, 20), "Mute")) 
                 {
                     if (AudioListener.volume == 0) 
                     {
                         AudioListener.volume = 1;
                     }
                     else 
                     {
                         AudioListener.volume = 0;
                     }
                 }
             
             if (GUI.Button (new Rect (Screen.width - 600, Screen.height - 20, 200, 20), "Pause")) 
                 {
                     pause = !pause;
                 }
             
             if (GUI.Button (new Rect (Screen.width - 400, Screen.height - 40, 200, 20), " " + mode + " Mode")) 
                 {
                     if (mode == "Build") 
                     {
                         mode = "Edit";
                     }
                 
                     else if (mode == "Edit") 
                     {
                         mode = "Build";
                     }
                 }
             
             if (GUI.Button (new Rect (Screen.width - 400, Screen.height - 20, 200, 20), "Reset Area")) 
                 {
                     Application.LoadLevel (Application.loadedLevelName);
                 }
             
             if (GUI.Button (new Rect (Screen.width - 200, Screen.height - 40, 200, 40), "Finish Layout")) 
                 {
                 
                 }
             
             if (mode == "Build") 
             {
                 GUI.Label (new Rect (0, Screen.height - 60, 200, 40), "Placement Angle: " + Controls.direction.y);
                 GUI.Label (new Rect (0, Screen.height - 40, 200, 40), "Reflectors Placed: " + Controls.count);
                 GUI.Label (new Rect (0, Screen.height - 20, 200, 40), "Trees Destroyed: 0");
             }
             
             if (mode == "Edit") 
             {
                 GUI.Label (new Rect (0, Screen.height - 60, 200, 40), "Selected Object: " + "None");
                 GUI.Label (new Rect (0, Screen.height - 40, 200, 40), "Angle (Yaw): " + Controls.direction.y);
                 GUI.Label (new Rect (0, Screen.height - 20, 200, 40), "Position (2D): " + Controls.selected.transform.position.x + ", " + Controls.selected.transform.position.z);
             }
             
             if (swap == false) 
             {
                 if (GUI.Button (new Rect (Screen.width - 200, 0, 200, 50), "Goals (Click to expand)")) 
                 {
                     swap = true;
                 }
             }
             
             if (swap == true) 
             {
                 if (GUI.Button (new Rect (Screen.width - 200, 0, 200, 300), "(Click to contract)" + "\n\n" + goals)) 
                 {
                     swap = false;
                 }
             }
         }
     }
 }

In the first scripts, it occurs at line 112, where it says

    if (Input.GetButtonDown ("Fire1") && rayhit.transform.name == "Reflector") 
             {
                 selected = rayhit.transform.parent;
                 angle = selected.eulerAngles.y;
             }

The other one appears in the second script at line 83, where it says

 GUI.Label (new Rect (0, Screen.height - 20, 200, 40), "Position (2D): " + Controls.selected.transform.position.x + ", " + Controls.selected.transform.position.z);

I know, it's obvious what the issue is, fixing is and isn't my issue, for the first one just put

 if (rayhit.transform.name != null) 

Right, well that does not work, at all, it just results in even more exceptions, I don't understand, I keep trying anything, but simple referencing these variables in any way results in a null reference exception in certain situations, I've been trying and trying, researching and researching, just to find out how to solve this.

I just don't get why it wont work.

It's probably something really simple and I'll once again feel like an idiot.

Comment
Add comment · Show 4
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 ArkaneX · Aug 21, 2013 at 07:25 PM 0
Share

Isn't the problem in the first script related to null rayhit? Did your raycast actually hit anything? Try to debug it, or put simple

 Debug.Log(rayhit);

before line 112

avatar image ArkaneX · Aug 21, 2013 at 07:29 PM 0
Share

And it seems, that if you only set selected in the first script, in line 114, then Controls.selected in second script is null.

avatar image Hugs Are Drugs · Aug 21, 2013 at 07:29 PM 0
Share

Yes, that's exactly the issue and I'm completely aware of that. If I click out into the void I get a NullReferenceException.

avatar image Hugs Are Drugs · Aug 21, 2013 at 07:31 PM 0
Share

oh now I think I get it. I feel dumb.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Aug 21, 2013 at 07:29 PM

Yeah you need to test up the chain, so to speak. If you have never used the debugger, get some sleep, and tomorrow learn to use the debugger, it will make you 100 times more efficient.

Until then, test

if (rayhit != null && rayhit.transform != null) // make sure these exist before checking the name

Likewise for Controls.selected.transform

You might also use print or Debug.Log statements in there as 'else' or something to print to the console what's going on.

Comment
Add comment · Show 2 · 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
avatar image Hugs Are Drugs · Aug 21, 2013 at 07:41 PM 0
Share

I made it so it checked if the raycast hit anything or not before executing, I should've known because at one point I tested to see if rayhit.transform.name was null and it wasn't, thanks for the help, but can you tell me how to go about using the debugger?

avatar image ArkaneX · Aug 21, 2013 at 07:52 PM 0
Share

Please take a look at official documentation. And in case of any issues, please post new question.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Object not existing after reloading scene (nullreferenceexception) 1 Answer

Unity event calling function gets nullreferencexception on bool 0 Answers

NullReferenceException script problem? 0 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