Screen.lockCursor Acting Irregular

Hello, I am using this script to control locking of the mouse in Unity WebPlayer, but there are some weird irregularities.

// for Standalone initial screen locking
     function Start ()
        { 
            Screen.lockCursor = true; // Start the game with the cursor locked or not 
        }
        function Update () {
            if(Screen.lockCursor == false && Input.GetKeyUp("p"))
            {
               Screen.lockCursor = true;
            }
            else if (Input.GetKeyUp("p"))
            {
               Screen.lockCursor = false;
            }
        }
    // Control Screen locking when focusing on the webplayer
        function OnApplicationFocus (){
         if(Screen.lockCursor == false){
         Screen.lockCursor = true;
         }
         }

When I load the webplayer the mouse is free to move the camera and I must click outside the player and then back to the player to get the mouse lock to activate (using Firefox). However the worst glitch is that I have a bunch of objects in the scene that I am using as triggers (for things like picking up items, turning on lights, starting animations etc) , and when the mouse is not visible from locking the cursor I cannot click any of them. When I start the game if I don’t click outside and back in to activate the focus part of the script I can click anything just fine (but the mouse is moving about the scene without being in the middle of course). Every time I toggle the script when locked and centered it I cannot click anywhere though looking around is normal, and as soon as I toggle it off I can click everything again. To make it even more confusing, using the exact same build in the standalone player and in the editor nothing is wrong at all. I would really appreciate help with this as I’ve spent an entire day trying different configurations to fix this.

var i:boolean = true;
function Start()
{
Screen.LockCursor = true; //Correct!
}

function Update()
{
     if(i)
     {
          Screen.LockCursor = true;
     }
     else if(i != true)
     {
          Screen.LockCursor = false;
     }
     if(Input.GetKeyUp(KeyCode.P))
     {
          if(i)
          {
               i = false;
          }
          else if(i != true)
          {
               i = true;
          }
     }
}

What I Think It’s Doing Is Not Rendering That Screen Is Currently Locked. So Here Is A Simple Declaration, Tested And Everything. Now When You Declare Your Focus, It Only Checks For It Being Locked. In This Case It Hasn’t Been Locked Already. So Now You Can Simply Put That Coding In At The End. Any Questions Feel Free To Ask!

I am not so sure about your description. I would guess what you mean is when playing if you click out of the webplayer you don’t control the game anymore. I am probably wrong though. If so, you need to use

function Awake(){
Application.runInBackground = true;
}

if not, I did not get it then. Also you can simplify your code with

function Start ()
        { 
            Screen.lockCursor = true; // Start the game with the cursor locked or not 
        }
        function Update () {
            if(Input.GetKeyUp("p"))
            {
               Screen.lockCursor = !Screen.lockCursor;
            }
        }
    // Control Screen locking when focusing on the webplayer
        function OnApplicationFocus (){
         if(!Screen.lockCursor){
            Screen.lockCursor = true;
         }
         }