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 Canine Enigma · Nov 04, 2014 at 06:28 PM · errorshooteraim

How to Fix "NullReferenceException"? Aim Down Sights Script

Hi community, I am kind of new to the whole Unity/progamming thing and am working on a school assignment.

With pretty much every script I've done, I have come across the error: "NullReferenceException", and, in most cases, have been quite successful in fixing the issue.

But, in the case of this AimDownSights Script, I have been less successful. I received the following error:

 NullReferenceException: Object reference not set to an instance of an object
 AimDownSights.Update () (at Assets/AimDownSights.js:44)

My script is as follows:

   var gun : Transform; 
   var curField = 60.0;
   private var dampVelocity2 = 0.4;
   var nextVector : Vector3; //Sights Transform
   var curVector : Vector3; //Normal Transform
   
   var curY = 234.8665; //Normal Rotation of Gun
   var nextY = 230.7; //Rotation of Gun when looking down Sights
  
   class weaponStats {
       var curDamage : int; //Normal Weapon Damage
       var nextDamage : int; //Sights Weapon Damage
       var gunScript : String;
       var mainCam : Crosshair_Script; 
   }

   var speed : float = 0.1;
   var stats : weaponStats;
   
   function Update () {
       //Smooth Changine Vield of View of Camera
      var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, curField, dampVelocity2, .3);
      Camera.main.fieldOfView = newField;
   
      if (Input.GetButton("Fire2")) { 
          //adjust viewpoint and gun position
          curField = 40;
          //Rotating Gun to look down Camera Perfectly
          transform.localRotation.eulerAngles = Vector3(0,nextY,0);
   
          //slow down turning and movement speed
          transform.root.GetComponent(CharacterMotor).movement.maxForwardSpeed = 2.5;
          //Adjust Gun Damage
          GetComponent(stats.gunScript).damage = stats.nextDamage;
          stats.mainCam.enabled = false;
          //Moving the gun
          transform.localPosition = Vector3.Lerp(transform.localPosition,nextVector, 0.3);
      } else {
          //adjust viewpoint and gun position
          curField = 60.0;
          transform.localRotation.eulerAngles = Vector3(0,curY,0);
          //speed up turning and movement speed
          transform.root.GetComponent(CharacterMotor).movement.maxForwardSpeed = 10;
          stats.mainCam.enabled = true;
          GetComponent(stats.gunScript).damage = stats.curDamage;
          transform.localPosition = Vector3.Lerp(transform.localPosition,curVector, speed);
      }
   }

So, I get that this means that the error is on line 44 and it probably has something to do with the "mainCam" but I am not sure how I would go about fixing this. Any help for this particular problem would be appreciated, and some information on how to solve this sort of problem in the future would probably make my life a lot easier.

Comment
Add comment · Show 1
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 AlwaysSunny · Nov 04, 2014 at 07:16 PM 0
Share

If we're seeing the same line 44, I would have to assume either the stats or the stats.mainCam variables are somehow null when that line executes. Place these statements before line 44 to confirm this:

Debug.Log(stats==null);
Debug.Log(stats.mainCam==null);

If either statement prints "true" you'll know that somehow one or the other are losing their references, and you'll need to figure out how and where by exa$$anonymous$$ing all statements in which the culprit variable is assigned to.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by AlwaysSunny · Nov 04, 2014 at 11:22 AM

Please correct the formatting on your posted code so we see the same "line 44" you see. Cool username, btw.

A null ref exception is just that - a variable you're trying to access points to something that isn't there. You can use Debug.Log() statements to print info to the console. Try printing Debug.Log("is myObject null? " + myObject==null); for instance, to determine which particular object is null.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

BCE0043: Unexpected Token: Contact Error 1 Answer

Cannot compile .exe with specific .dll. (but with other .dlls created the same way) 0 Answers

Enter Trigger, display Text, then delete object 1 Answer

Zombie Round Script Help 1 Answer

2D bullet script errors. 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