Why does gameobject.Find/GetComponent not work when a new scene is loaded?

I’m transitioning from from LandingPage scene to ItemFocusTest scene with a singleton script that uses Scenemanager.Loadscene called from a button.

When Unity loads up the ItemFocusTest scene, certain scripts/gameobjects seem to fail to get references to other objects and components in the scene, preventing the scene from working properly.

It’s important to note that ItemFocusTest works correctly when loaded manually in the Unity Editor - meshRenderers are found, their material.colors set to color.clear and a reference is cached to the TileClicker script on the Main Camera. (Look for all-caps below)

void Start () {
        tapTiles                            = tileParent.GetComponentsInChildren<TapTile>();            // Get an array of all the tiles
        oneOfTheTileRenderers               = tapTiles[0].GetComponent<MeshRenderer>();                 // Get the meshrenderer of one of the tiles
        tileMat                             = oneOfTheTileRenderers.sharedMaterial;                     // Find the shared material of all the tiles - so when we fade the material, it fades on all the tiles
        tileMat.color                       = opague;                                                   // Make sure the material is fully visible when the scene loads
        touchIcon                           = GameObject.Find("TouchIcon");

        imageFaderMat                       = GameObject.Find("ImageFader").GetComponent<MeshRenderer>().sharedMaterial; // **THIS REFERENCE ISN'T BEING CACHED!**
        imageFaderMat.color                 = Color.clear;

        tileClicker                         = GameObject.Find("Main Camera").GetComponent<TileClicker>(); // **NEITHER IS THIS ONE!**
    }

On loading ItemFocusTest from LandingPage, I get the following error message:

MissingComponentException: There is no ‘MeshRenderer’ attached to the “ImageFader” game object, but a script is trying to access it.
You probably need to add a MeshRenderer to the game object “ImageFader”. Or your script needs to check if the component is attached before using it.
TileCounter.Start () (at Assets/Scripts/Props/TileCounter.cs:41)

At a certain point in itemFocusTest, using enough items SHOULD trigger a set of fades and turn off clicking interaction. Instead, I get the following error…

NullReferenceException: Object reference not set to an instance of an object
TileCounter.AddOneToCount () (at Assets/Scripts/Props/TileCounter.cs:73)
TapTile.TapInteraction () (at Assets/Scripts/Props/TapTile.cs:44)
TileClicker.TapOnTile (RaycastHit target) (at Assets/Scripts/Camera/TileClicker.cs:99)
TileClicker.TileIntMouse () (at Assets/Scripts/Camera/TileClicker.cs:76)
TileClicker.TileInteraction () (at Assets/Scripts/Camera/TileClicker.cs:41)
TileClicker.Update () (at Assets/Scripts/Camera/TileClicker.cs:28)

Again, all this works when the scene is loaded in isolation in the Unity Editor.

What’s going on? What’s stopping the script from finding the correct gameObjects and/or components? Is there a later event than Start that I can use for initialization? I’m baffled.

–Rev

PS: In passing, there’s a question I asked earlier that appears to be held up for moderation - I’ve no idea why. If there are any mods around, could you take a look at this? http://answers.unity3d.com/questions/1235843/why-will-my-webgl-project-with-streaming-video-not.html

This problem remains unsolved, but a workaround is to make the meshrenderer variable public and just assign it in the Inspector. Technically this is worse than doing everything through scripting (I think!), but you can’t argue with something that just works.

–Rev