- Home /
Parsing Error, code in android for a button
Hello. I'm a completely beginner in unity and scripting. But I see tutorials and etc and I'm learning a lot. I'm now doing an Android Game. Now I have a code that will press a button and will for example do something like Load a Scene to start the game. A Start Game button and a Quit Game button.
My code is this:
using UnityEngine;
using System.Collections;
var isQuitButton = false
public class TestButton : TouchLogicV2
{
public bool disableTouches;
public override void Update()//(optional) only use Update if you need to
{
//you can do some logic before you check for the touches on screen
if(!disableTouches)//(optional) dynamically change whether or not to check for touches
base.Update();//must have this somewhere or TouchLogicV2's Update will be totally overwritten by this class's Update
}
public override void OnTouchBegan()
{
if( isQuitButton )
{
//QuitTheGame
Application.Quit();
}
else
{
//LoadTheGame
Application.LoadLevel("Test");
}
}
}
But this gives me a Parsing Error... I think the error is inside public override void OnTouchBegan() and I say this because this other code works good:
using UnityEngine;
using System.Collections;
public class TestButton : TouchLogicV2
{
public bool disableTouches;
public override void Update()//(optional) only use Update if you need to
{
//you can do some logic before you check for the touches on screen
if(!disableTouches)//(optional) dynamically change whether or not to check for touches
base.Update();//must have this somewhere or TouchLogicV2's Update will be totally overwritten by this class's Update
}
public override void OnTouchBegan()
{
print ("HELLO FROM TEST BUTTON!");
}
}
Can you explain me what am I doing wrong in a beginner form? Or can you say me a form to create a button that Starts the game and another to quit the game like a Main Menu? Thanks in advance!
Answer by MakinStuffLookGood · Jan 07, 2015 at 07:12 PM
You have a variable defined outside the class missing a semicolon. The full error message should point you directly to that.
Just move:
var isQuitButton = false
into the scope of the class, and append a semicolon.
Answer by Ed unity · Jan 07, 2015 at 07:14 PM
You are missing a ; after the declaration of your variable and it is outside the scope of the class. It should be the following:
var isQuitButton = false;
However, in your code, please also keep in mind that nothing is changing the isQuitButton variable to true and thus, Application.Quit() will never be called.
Thank you very much, now my script works, I'm trying to work with a 3D Text with a Box Collider but that doesn't work.. but the script problem is solved. The Variable isQuitButton is if in the main menu the button is called Quit, then you change it on the Inspector.