GameObject.Find("...").GetComponent<Slider>(); Doesn't work?

Hi! I am working on the script, so it automaticly puts in the object it needs. It works fine for all eccept for theese two, which are marked and // I cant any tipping errors and I have rewrote them already a few times. no change. it always says:

NullReferenceException: Object reference not set to an instance of an object
AutoPilot.Awake () (at Assets/AutoPilot.cs:48)

NullReferenceException: Object reference not set to an instance of an object
AutoPilot.Awake () (at Assets/AutoPilot.cs: 52)

And yes I removed the //

8415198--1112565--upload_2022-9-5_0-23-21.png

You really should not be using GameObject.Find for hierarchies nested so deep. And honestly, you shouldn’t be using it at all.

Does all this live in the same scene? Can you not set up references to all of these objects in the inspector?

yes, It’s all in 1 scene. BUT its a llot of work and its way easier to make it this way 1 time instead of the other way in the inspectior like 20 times

And what if you change the hierarchy? Then you have to go through this nonsense again, dealing with the issues you are dealing with now.

It’s your time spent mate, but there’s a good reason why we can reference objects and components in the inspector.

yea sure! but why does it work with everything eccept theese two?

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

Same rules as always:

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums