- Home /
Problem with "Pick up key" script
I've started Unity and scripting a week ago so please, feel free to treat me like an idiot ^^ I threw a javascript together that's supposed to show a message when you click on a key, and then lets you choose whether to pick it up.
private var gotKey : boolean = false; private var showMsg : boolean = false;
function OnMouseDown() { showMsg = true; function OnGUI () { if (showMsg = true) { GUI.skin.box.wordWrap = true;
GUI.Box(Rect(100,50,500,200),"It's the key to the door that leads to the mysterious and unknown outside wooooooorld. \n(Press E to pick it up, press R to cancel)"); } } function Update() { if (Input.GetButtonDown ("e")) { gotKey = true; Destroy (gameObject.Find("key"));
} if(Input.GetButtonDown ("r")) { showMsg = false; } }
}
It looks fine to me. But I get this error message: Assets/Scripts/getKey.js(8,18): BCE0044: expecting (, found 'OnGUI'. So it expects me to just write function()? Please help...
Answer by Jake-L · Mar 13, 2011 at 12:52 PM
Your OnGUI() and Update() are inside your OnMouseDown() function. Place each as a class member and you're almost fine. Note that Input.GetButtonDown returns the mouse button, not the keys. You want to use Input.GetKeyDown
...and how would I do that? I'm sorry, I know how to create classes but I don't really understand how to use them / what they're good for :<
NEVER$$anonymous$$IND. I just pulled them out of each other and added the if-Statement everywhere. All I needed to know was that functions can't be within each other. Thanks.
Answer by Dbag · Mar 19, 2011 at 08:37 PM
showMSG = true; comes up as an error. it wants to change it to showMSG : true;
Your answer
Follow this Question
Related Questions
Script error. Please Help! 4 Answers
'If' Statement in javascript giving me problems 3 Answers
BCE0044: expecting ':' found ';' 1 Answer
Basic on collision play animation code not working 1 Answer
BCE0044: expecting :, found '=' 3 Answers