My epiphany

I just had an epiphany! It’s so nice when a concept just hits you in the face.

Here it is:

Anytime you are working or editing in a script and you call a different script such as:

In the Level script we added:

SceneLoader sceneLoader;

private void Start()
    {
        sceneLoader = FindObjectOfType<SceneLoader>();
    }

Without the FindObjectOfType, Level script could not access SceneLoader script.

There is a pattern here.

We did it also in the Blocks scripts with Level.

 // Cached reference
    Level level;   
    
    private void Start()
    {
        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks();
    }

So I guess in conclusion I could say "Anytime we have a cached reference to a different script, we have to have private void Start with the FindObjectOfType linking to the cached reference.

Jenn

2 Likes

Privacy & Terms