[SOLVED] FindObjectOfType doesn't seem to work for me

i’ve followed Ben’s lesson to the T, but when it came time to using FindObjectOfType in my LoseCollider script I’m still getting the NullReferenceException.

Here is my code:
FindObjectOfTypeCode

This is the error I get when the Ball triggers the LoseCollider:
UnityError

I’m at a loss as to where I went wrong and to me everything looks right…

Do you have a LevelManage GameObject in your hierarchy with LevelManager script attached to it ?

Or you can also turn your variable to ‘public’ and in your editor drag your LevelManager object in the exposed field

Hey, thanks for the quick reply Dieedi!

I do have the LevelManager object in my hierarchy with the proper script attached. I have just made it public and dragged the object into the field just so I can continue in the lessons, but I really want to know why my scripts don’t work properly. Especially since I’ve been following Ben and exactly what he’s been doing…

Yes it’s weird, I’ve just checked my old script for same project and it works fine …

If you try a Debug.Log(levelManager); replacing your call to LoadLevel is there the same error ? to check if he gets it

I put in the Debug.Log and this is the result that I got.

UnityError2

I don’t remember using FindObjectOfType with the GameObject keyword, have tried calling it without the GameObject before?

Still didn’t work. It’s weird because the same line of code worked when you used for the Ball script and it worked fine. Might it be a bug in my project or something?

How about the space between the findobjectodtype and <>?
I mean, don’t seems to be any other different thing besides these two.

I have the space on my Ball script and it doesn’t seem to make a difference, but I removed it just to see and it’s still broken. I think maybe my project just has a corruption or a bug or something.

Well, isn’t the script duplicated? Have you tried restarting Unity? It don’t make sense since you are able to run it by dragging to the inspector (the more common error is when it is not in the hierarchy)

Yeah I don’t know what’s going on. Tried restarting and it’s still not working. I rewrote the script as a separate script file and tried using that and it’s the same problem. At this point, I have no idea what else to do.

I don’t think that it would fix it, but have you tried assign the levelManager in the Start() or OnEnable() instead?
I’m running out of ideas xD

Haha yeah me too! I had already tried adding it to Start, but it didn’t work. :frowning:

Sorry for not being able to help, i have no idea why it don’t want to work

Me neither! Thanks for the help though! I am having the same problem trying to use the FindObjectOfType on the bricks to pass the win condition too… I’m so lost right now, but at least if I make the LevelManager public I can put the object in via the inspector and it works. It’s just going to take a lot longer to make levels.

Are you sure there is only one LevelManager on the hierarchy? This could be the problem too

Yeah, as far as I can see there is only 1. This is with LevelManager being public so I can continue with the classes.

Would you mind trying one last thing?
copy and paste this in the Start() method:

Debug.Log(FindObjectsOfType<LevelManager>().Length);

What appears in the console when you enter play mode?

Getting this error:

Assets/Scripts/LoseCollider.cs(9,46): error CS1061: Type LevelManager' does not contain a definition forLength’ and no extension method Length' of typeLevelManager’ could be found. Are you missing an assembly reference?

there is an “s” in there, isn’t the normal FindObjectOfType, this method should return the number of levelmanagers in the hierarchy