Clarification on FindObjectOfType<>(Quiz) line

Everything is working okay so there is no problem as such. I’m just looking for a bit of clarification on how the FindObjectOfType() method is working with regard to a Canvas. This is best explained by example.

In the GameManager class we a variable declaration - Quiz quiz;

By this my understanding is that we are creating an instance of Quiz class (as defined in the Quiz.cs script). So far so good.

Then in the GameManager class we assign the quiz variable using FindObjectOfType to look for an object of type Quiz.

This is followed by - quiz.gameObject.SetActive(true);

The QuizCanvas is then set to active and appears. My question is that if we are finding an object of type Quiz then surely that is the class that we are looking for and not the Canvas gameObject itself.

I’m thinking that the way it must be working is that it is looking for an instance of the Quiz class which it then finds attached to the QuizCanvas and this is how it knows to set the QuizCanvas to active.

I hope that makes sense. Is my understanding correct or am I missing something?

Grateful for any help or clarification.

Stephen

Yes, that’s pretty much it.

The FindObjectOfType looks through the whole hierarchy until it finds a component called Quiz and returns it. The component is attached to a game object, so the line quiz.gameObject refers to the game object that quiz component is attached to. This is the same game object that the canvas is attached to (canvas is just another component) and calling SetActive(true) on the game object enables the whole game object

Thanks a million for the quick response. Sorry about the noob question but it’s good to fully understand what’s going rather than just assuming.

Cheers,

Stephen

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms