I need a function so that when and event is trigger a message is displayed on the screen for a set duration.

something like this

function message(msg, duration)
{
// code i need to display message(msg) for duration (duration) seconds
}

then when something happens i want to do something like this
message(“you picked up the box” , 2);

you can try this:

var showMsg = false;
var msgText:string = "";

function OnGUI()
{
     if(showMsg)
     {
          GUI.Label(Rect(100,100,200,100),msgText);
     }
}

function ShowMessage(msg:string,duration:float)
{
     showMsg = true;
     msgText = msg;
     yield WaitForSeconds (duration);
     showMsg = false;
}

// you can call ShowMessage() from anywhere you want, I have called on Start()
function Start()
{
    ShowMessage("This is your message",2);
}