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
-2
Question by awplays49 · Jan 21, 2015 at 11:14 PM ·

NullReferenceExceptions. Dont know how to fix...

this code attached to field of view slider...

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class FieldOfViewSlider1 : MonoBehaviour {
 
     private Slider Slider;
 
     void Start () {
         Slider.value = Camera.main.fieldOfView;
     }
 
     void Update () {
         
     }
 }

and this to camera...

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class MainCamera : MonoBehaviour {
     
     private float FieldOfView;
 
     void Start () {
         FieldOfView = PlayerPrefs.GetFloat ("FieldOfView");
         Camera.main.fieldOfView = FieldOfView * 10;
     }
 
     void Update () {
         FieldOfView = GameObject.Find ("Field Of View Slider").GetComponent <Slider> ().value;
     }
 
     void OnApplicationQuit () {
         PlayerPrefs.SetFloat ("FieldOfView", FieldOfView);
     }
 }
 

very broken, as you can see. im trying to save the fov variable.

Comment
Add comment · Show 28
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 Superrodan · Jan 21, 2015 at 11:22 PM 0
Share

What line is the null reference exception co$$anonymous$$g from? In the debug log if you click on the error you should be able to narrow it down to a script and line number.

avatar image awplays49 · Jan 21, 2015 at 11:42 PM 0
Share
 Slider.value = PlayerPrefs.GetFloat ("FieldOfView1", 5);
avatar image awplays49 · Jan 21, 2015 at 11:42 PM 0
Share

that in start and

 PlayerPrefs.SetFloat ("FieldOfView1", Slider.value);

in application quit function

avatar image Superrodan · Jan 22, 2015 at 12:00 AM 1
Share

Trying to help you is a bit like trying to hit a moving target because the code keeps changing between your updates. I would appreciate if you post the two scripts how they are currently, as well as the lines that are giving you the errors, then do not change the scripts until I have a chance to look at all three.

avatar image DaDonik · Jan 22, 2015 at 12:29 AM 1
Share

You haven't even started the program$$anonymous$$g. You have to be a tough guy and bite some books. Thats how all experienced ppl started. You can't use a game engine like unity and expect to learn program$$anonymous$$g on the fly, asking random guys on the internet (thats us). I haven't touched a game engine the first months of 'learning how to program'. I haven't said 'program$$anonymous$$g' on purpose, because you have to start at the very start and not directly in gamedev. Of yourse Unity is meant to be easy for newcomers, but imho easy is no term that should be used in the same sentence as program$$anonymous$$g.

Please note that i'm not trying to be rude here, i just have a decade more experience in the matter than you have, and i learned it from books, then i graduated in the subject and now i'm what i would call 'somewhat experienced programmer...

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by InvincibleCat · Jan 22, 2015 at 12:07 AM

private Slider Slider

Should be public And assign the slider from the inspector

Comment
Add comment · Show 11 · 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 DaDonik · Jan 22, 2015 at 12:10 AM 1
Share

If awplays49 had taken my advice over a month ago, those problems would not be a subject for discussion. I adviced him to read a book on program$$anonymous$$g...

Na$$anonymous$$g a variable exactly like the type of the variable....cmon...

avatar image dreamothee · Jan 22, 2015 at 12:13 AM 0
Share

But @awplays49 said he knows a lot on program$$anonymous$$g ! Even if he should definitely start by learn how to program... There is a long way still

avatar image awplays49 · Jan 22, 2015 at 12:13 AM 0
Share

Yea cause that's helpful... If you had listened to me a FEW months ago you'd know I have a special way of understanding things that you shouldn't question.

avatar image DanSuperGP · Jan 22, 2015 at 12:17 AM 1
Share

head desk

avatar image DaDonik · Jan 22, 2015 at 12:23 AM 1
Share

The truth is always hard...but true :D

Show more comments
avatar image
0

Answer by Noah Dyer · Jan 22, 2015 at 12:21 AM

I see what you're trying to do. So let's take this one by one.

First, your slider is in fact null during the start function of FieldOfViewSlider1. When you initialize it, so far as your script is concerned, it's null, and then you try to access it before you do.

To solve this, update as below .

  private Slider fovSlider;
 
  void Start () {
      fovSlider = GetCompoent<Slider>();
      fovSlider.value = Camera.main.fieldOfView;
  }

Secondly, in your MainCamera at Start(), you've never set a player pref, so when you try to assign it, it is in fact null. To avoid this, only assign it if the key has been set. Like the following:

 if (PlayerPrefs.HasKey("FieldOfView"){
     FieldOfView = PlayerPrefs.GetFloat ("FieldOfView");
 }

Lastly, I think your logic is just generally a little funky and overcomplicated. All of this could be handled in your FieldOfViewSlider1 Script/object, saving you the Find and Get Component Calls of your Update.

Best of luck!

Comment
Add comment · Show 1 · 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 awplays49 · Jan 22, 2015 at 12:56 AM 0
Share

its okay, i completely forgot giving fovslider a value, my fault. completely slipped my $$anonymous$$d.

avatar image
0

Answer by DanSuperGP · Jan 22, 2015 at 12:28 AM

Null Reference Exception means that something you are trying to reference is null.

So when it tells you which line is giving you the null reference exception, then you know that whatever object is in that line of code isn't actually referencing anything.

In this particular case, it's right at the beginning...

 public class FieldOfViewSlider1 : MonoBehaviour {
  
      private Slider Slider;
  
      void Start () {
          Slider.value = Camera.main.fieldOfView;
      }

You have a private value Slider (which should totally not be named that... ) that you have never assigned a value to. Then immediately afterwards you are trying to assign to the value of that uninitialized variable.

Also, you're doing this in your other script.

      void Update () {
          FieldOfView = GameObject.Find ("Field Of View Slider").GetComponent <Slider> ().value;
      }

This is a HUGE mistake. You should do GameObject.Find at most once, ever... on start... and store the object you get back in a local variable.

Comment
Add comment · Show 5 · 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 awplays49 · Jan 22, 2015 at 12:55 AM 0
Share

thats what i forgot, the game object.find, which i usually do.. i don't know what happened but i totally forgot and couldn't see the problem

avatar image DanSuperGP · Jan 22, 2015 at 01:02 AM 1
Share

Great, glad you found the problem. You should also address the second problem I mentioned where you are doing GameObject.Find inside of an update function. GameObject.Find is EXTRE$$anonymous$$ELY SLOW and should only ever be done on startup, if at all.

I never use it for anything.

avatar image InvincibleCat · Jan 22, 2015 at 01:04 AM 1
Share

I agree GameObject.Find should never be used. Only if someone is threating you!

avatar image DanSuperGP · Jan 22, 2015 at 01:15 AM 0
Share

Nobody would ever dare threaten me... I'll beat them with my +5 mace of dependency injection and then cast Inversion Of Control

avatar image awplays49 · Jan 22, 2015 at 01:57 AM 0
Share

thanks for the advice

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

30 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 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

"Error in text editor extension chain." when trying to edit new UnityScript file 3 Answers

Is this code right? -1 Answers

Built Game Crashes But Not On Editor 0 Answers

Adjusting skin weights = broken mesh 3 Answers

Lighting is making my scene look crazy/spikey/broken. 2 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