x


Game is skipping from main menu into playable level without the 'start' button being pressed.

I've been having some problems getting the name display to work, turns out I just needed some curly braces in there. However now when I load my main menu the scene is skipping straight into my main level scene without the 'start' button being pressed. The scene loads for a couple of seconds then skips. This aspect of the scene was working perfectly until I input the new code, so I have a feeling I've put the new script in the wrong place.

if (GUI.Button (new Rect(Screen.width/2-buttonWidth/2,
           buttonyPosition,buttonWidth,buttonHeight), "Start"))
    {
    GameObject gameData=GameObject.Find("GameData");
    if (gameData!=null)
       {
         GameDataScript gameDataScript=gameData.GetComponent<GameDataScript>();
         gameDataScript.playerName=nameText;
       }
    }
    {
       //load the level
       StartCoroutine(PlayButtonClick());
       Application.LoadLevel("PhysicsLab");
    }

The new code causing the problem is the part about GameObjects.

Any help would be greatly appreciated!

more ▼

asked Dec 06 '11 at 11:54 AM

pretzelhead gravatar image

pretzelhead
1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Of course it does. Align your brackets and you will see that your bottom most code block is just outside your if statement:

if (GUI.Button (new Rect(Screen.width/2-buttonWidth/2,
       buttonyPosition,buttonWidth,buttonHeight), "Start"))
{
    GameObject gameData=GameObject.Find("GameData");
    if (gameData!=null)
    {
        GameDataScript gameDataScript=gameData.GetComponent<GameDataScript>();
        gameDataScript.playerName=nameText;
    }
}
// THIS IS NOT WITHIN THE BUTTON IF BLOCK
{
   //load the level
   StartCoroutine(PlayButtonClick());
   Application.LoadLevel("PhysicsLab");
}

I can't tell how you want / need this code fraction to be, but it's quite obvious that's your problem. Probably you just want the Loadlevel piece just inside the if block.

more ▼

answered Dec 06 '11 at 11:58 AM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

Amazing, thank you! Works perfectly now. Thanks for the quick reply as well.

Dec 06 '11 at 12:05 PM pretzelhead
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5068
x10

asked: Dec 06 '11 at 11:54 AM

Seen: 317 times

Last Updated: Dec 06 '11 at 12:05 PM