- Home /
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.
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.
Slider.value = PlayerPrefs.GetFloat ("FieldOfView1", 5);
that in start and
PlayerPrefs.SetFloat ("FieldOfView1", Slider.value);
in application quit function
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.
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...
Answer by InvincibleCat · Jan 22, 2015 at 12:07 AM
private Slider Slider
Should be public And assign the slider from the inspector
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...
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
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.
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!
its okay, i completely forgot giving fovslider a value, my fault. completely slipped my $$anonymous$$d.
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.
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
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.
I agree GameObject.Find should never be used. Only if someone is threating you!
Nobody would ever dare threaten me... I'll beat them with my +5 mace of dependency injection and then cast Inversion Of Control
Your answer
