How to make a loading feedback for big GameObjects loading?

I already have one answer to a part of this question: by using the yield statement, for example:

private var loading : boolean = false;
function loadMyMesh()
{
    loading = true;
    var myNewGO : GameObject = Instantiate(Resources.Load("models/bigbigmesh", GameObject));
    yield;
    loading = false;
    return myNewGO;
}
function OnGUI()
{
    if (loading)
    {
        GUI.Box(Rect(0,0,50,10), "LOADING...");
    }
}

This is the first time I use “yield”, and I have not understood everything about coroutines yet.
I’m not sure this is the best workaround for my problem, and whereas this code works fine in the editor, this doesn’t work while deployed for the web player. If I try to load something on the web player, the object is loaded, then the message “loading…” appears for one frame, then disappears.

What am I doing wrong?

I don’t know how to use yield in this way. In my code I’m just using something like this:

using UnityEngine;
using System.Collections;

public class sas : MonoBehaviour {
	
	bool loading = true;
	GameObject SomeObject;
	
	void Update () {
		if(SomeObject == null){
			SomeObject = GameObject.Find("Name of this object");
			loading = true;
		}
		else
			loading = false;
	}
}

hope it will help.

Ok I got it working.
This is the steps I followed:

  • you made me understand the following thing: at the line an Instantiate or a Resources.Load is called, no Update or OnGUI is called until it’s done. That’s why my line “loading = true;” was not taken into account in the OnGUI monobehaviour until the loading was done.

  • so, I changed my code to the following:

    function loadMyMesh()
    {
    loading = true;
    yield;
    var myNewGO : GameObject = Instantiate(Resources.Load(“models/bigbigmesh”, GameObject));
    loading = false;
    return myNewGO;
    }

It’s still working in the editor, but a strange problem came:
the function “loadMyMesh” doesn’t have the same behaviour when it is called by the navigator through javascript “SendMessage” (see [here][1]) as when the same function is bound to an event such as Input.GetKeyDown()
In the first case, the yield; doesn’t make any difference, whereas it’s working fine when the function is bound to a keyboard event.
To solve the problem with the least modifications, I first tried the meat500’s solution, which worked out perfectly, thus I still don’t really understand what’s been going on. Anyway, here is my final code:

function loadMyMesh()
{
    loading = true;
    yield WaitForSeconds(.1);
    var myNewGO : GameObject = Instantiate(Resources.Load("models/bigbigmesh", GameObject));
    loading = false;
    return myNewGO;
}

Thank you for your help!
[1]: http://docs.unity3d.com/Documentation/Manual/UnityWebPlayerandbrowsercommunication.html