mouse controls in a main menu

hello, i am having a terrible time trying to get my mouse sensitivity control and my invert mouse Y options to work. i keep getting nullreferenceexeptions and errors. can anyone tell me what im doing wrong?
the mouse sensitivity float is in another script, but its just not working!

private PlayerLook PLook;


   [Header ("Gameplay Settings")]
  [SerializeField] private TMP_Text mouseSenseTextValue = null;
  [SerializeField] private Slider MouseSensiSlider = null;
  [SerializeField] private int defaultSens = 4;
  public int mainMouseSens = 4;


private void Awake()
{
  
    PLook = GetComponent<PlayerLook>();
          
}



public void SetMouseSens(float sensitivity)
    {
        mainMouseSens = Mathf.RoundToInt(sensitivity);
        mouseSenseTextValue.text = sensitivity.ToString("0");

    
        PLook.sensY = MouseSensiSlider.value;
     

    }

the error is in the last line. its like it cant see the value in my PlayerLook script. what the heck am i doing wrong??

Steps to solve a null error are always the same.

  1. Find what is null
  2. Find why it’s null
  3. Fix it!

Sounds like your PLook is null, which means your GetComponent call isn’t returning anything. GetComponent always looks at the same gameobject as the script that is calling it is on. So, if that script isn’t there, it is null.

3 Likes

@Brathnann Steps weren’t clear, and now I’m a null.

1 Like

Of course, dang, why didn’t I thinkto check that?? I’ll fix it tonight and see what happens! I’ll check back!

@Brathnann ok i fixed the line to say PLook = GameObject.Find("Player").GetComponent<PlayerLook>(); but it is still the same error. object reference not set to instance of object. is tgis because my player game object is in another scene?
basicAlly im trying to create a main menu that controls mouse sensitivity. but im having a hard time finding a basic how-to

Break it up into more lines to figure out which of the two calls is returning null. Either GameObject.Find OR GetComponent<T> could be returning null. Then you can investigate why either or isn’t being found.

In any case there’s an architectural issue here that’s going to make things difficult in the long run. The settings menu should be a layer above the actual settings values, and should not at all touch the actual player controller components.

Think about it this way. You have a blob of data that is all the various settings values. The player reads from these values, and the settings menu mutates these values.

The most basic method would be to put the settings into a static class.

ok so is there a tutorial out there that could help me set this up? i was hoping to make a main menu with all these settings to change and then go play the game. also, how the heck to you control brightness of the screen??

i think it isnt being found because my player game object is in another scene

Don’t rely too heavily on tutorials. Break it down into the problems you need to solve, and learn how to do each individual step before combining that knowledge. Don’t know about static class? Look up the C# documention. Don’t know how to write data out to a file? Look up a tutorial on that.

You still need to break down that line to test which of the two calls is returning null. Otherwise you’re just guessing and will get exactly nowhere. See the first response to your thread.

GameObject.Find will look through all scenes, so the only reason it would fail is if there is no game object called “Player”, or the scene the player is in hasn’t been loaded yet. Same with GetComponent<T>. The game object you might be getting (again, you don’t know) might not have the desired component.

In short, you need to debug your code rather than guessing.

yeah ive kinda given up on tutorials cause they require a whole new scene and scripts for each.

so if the scene with the player hasnt been loaded, can i use something else to find that script and change a variable in it? ive got a “multiplier” float that i could change that would work for mouse sensitivity, but the only task is getting to it and changing it before its loaded.

In short, no.

In long: If the scene hasn’t load it doesn’t exist. You cannot access anything nor change anything about it. It’s just not there at all.

When you load a scene, everything is instantiated from the asset file into memory. When it’s unloaded, everything is destroyed never to be seen again. And if you load the scene again, it’s a whole new copy with no relation to the previous instance.

Hence this:

Have your settings adjust values in a static class. These live independant of scenes and the values will persist between scene loads (and also between play mode sessions, so keep that in mind). Your settings can change these values, and your player controller can read from it. Easy done.

ok i will try to do that. and i can change these settings in main menu as well right? now is there any kind of info on making this static class?

::EDIT:: JUST made a static class. works! thank you guys!

i can change the sensitivity and everything! it actually works!

now a dumb question: anyone know why when i play my game and hit esc, the menu locks up and i cant see my mouse?

I would start by googling “C# static class” and see where that takes you.

Not trying to be arsey, but just searching up “C# [thing I don’t know about]” is how I learnt probably 90% of what I know.

That depends on the input system being used, and the accompanying event system. But it’s just a case of setting up an event system that only looks for arrow key inputs.