- Home /
Problem with button On Click
I am using unity 4.6.3. I am new to unity so I watched several video tutorials on youtube all the step i did that was in tutorial. I tried everything but I can't find solution of my problem.
In my project I created a scene "MainMenu" In this scene there are two buttons named "Start Game" & "Exit Game". There is an empty game object also with position (0,0,0) and add a script "mainMenuButtons" to it.
Here is my script "mainMenuButtons"
public class mainMenuButtons : MonoBehaviour {
void StartGame(){
Application.LoadLevel (1);
}
void ExitGame(){
Application.Quit ();
}
}
Now I want to assign on click event on "Start Game" buttons to this StartGame() function. So I followed the steps click on StartButton. Click on "+" sign below the On Click(). When I click "+" sign I show 3 items here is a screen shot
I draged an empty game object i.e. UIManager. click on No Function and select a script "mainMenuButtons" but there is no StartGame() function or ExitGame() function.
Here is a screenshot
All the tutorial show the StartGame function but in my case it is not
am i making any mistake? plz help me
sorry for my english & Thanks in Advance
Answer by Bhargav5530 · Apr 14, 2015 at 10:29 PM
I found answer my self. This may help others who are new to unity3d. Just add a public keyword. by default the function in unity is private and if you declare a private function then the outside the unity inspector can't use those function. so you need to assign a public function.
Here is my script and it works perfectly.
public class mainMenuButtons : MonoBehaviour {
public void StartGame(){
Application.LoadLevel (1);
}
public void ExitGame(){
Application.Quit ();
}