Button in unity doesn't work, how can i fix it? - c#

I am new to unity and I am still learning. I am making a game and its going great but I have a problem with a Quit button.
It doesn't use the script attached to the On Click () section in the button menu.
There's an EventSystem in the Hierarchy, the Force Module Active is checked, the button is a child to a canvas. I don't know why the button isn't working
Here is the code in the script attached to On Click ()
public void Quit()
{
Debug.Log ("YES IT WORKS");
Application.Quit ();
}
I want the program to close when the button is pressed but when the button is pressed nothing happens!

The function is not being called when the button is pressed which means that you have not selected the function in the onClick settings. To do this, ensure that the script is attached to the button and then dragged into the onClick box of the button in the inspector. Next, click on the dropdown and hover over the name of the attached script and select the function that you want to call.

Solution
Instead of using Application.Quit() try to shift the your working scene to the home screen .Your every game should start and end at the same level which is also known as the main scene of the the game. Secondly before shifting to other scene you should destroy your player and save the scores in the player pref .
Player Pref:
The library which helps the user to store the data and show them on the main screen , Hence the use of database will not be there in order to save the data for the small games . You are beginner so you should look at these all thing on You tube or the unity website documentation available on its website

Are you in the Editor?
Application.Quit() only works when you build the application (for PC and Android. It should not work on iOS, and even if it works it should display as a crash so it's not good).

Related

Event Trigger isn't working on my Image with Unity

I am using image (Amazon Icon) as button with Event Trigger Pointer Click component but it's not working when I click.Sometimes it's working while clicking one specific place at icon but I don't know what is triggering it.Is there any way to make it work? I am not able to use button because of my canvas is small and it's being blurry when I use buttons.
pcinteract script:
`
public void start(){
number++;
Debug.Log("Clicked:" + number);
}
Screenshot from game screen
Inspector panel of Amazon Icon
working area
It appears that there may be an issue with the event trigger component on the Amazon icon game object. To troubleshoot this, you can try the following steps:
Verify the presence of a collider component on the Amazon icon game object. If it is not present, add it so that clicks can be detected.
Ensure that the "Is Trigger" option within the collider component has been checked.
Scrutinize the order of components in the event trigger component, making certain that the "Pointer Click" component is above the others.
Consider incorporating a Debug.Log() statement into the start method, to determine if the method is being
called upon clicking the Amazon icon.
If these steps prove ineffective, you may want to experiment with creating a new game object and adding an image component, collider component, and event trigger component to it. Test clicking the new object to see if it functions as intended.

I can't assign scripts to buttons in UNITY?

I just started exploring Unity and I'm making a game in UNITY 2D.
I decided to start with a simple menu screen to get used to UNITY.
I made the menu and moved on to programming the buttons.
However when I drag the script (or the game object holding the script) in to the button on click section and try and select my function it does not show up.
My unity window
Manager in the button on click script.
My code in C# (this is my first time using c#)
what I want to know is: have I done something wrong; doe unity work differently to how I understand it; is unity doing something weird; how do I fix it.
(appologies for the weird post layout I don't know why it did that).
Just insert a ; at the end of your code and you should be fine. You should notice that the compiler is complaining about the missing semicolon. Also, make sure to attach your script to the _Manager GameObject.
You need to :
add a semi colon to the end of the line in the Buttons class
Add Buttons Class to the _Manager gameobject in the scene

Load the last played scene where user lost instead of loading the start scene all over again, Unity Block breaker game

I have made block breaker game in unity 4.6 and i have added 3 levels. If anyone loses in any level say level_02 then the lose screen showing game over loads up. When he clicks try again it loads the start screen as i have ordered them in my build settings. I want my game to load the same level where the user have lost so that he clicks try again and the same level loads again i.e level_02.
You can save the current levelScene name before you loads the looseScene like,
PlayerPrefs.SetString("LastLevelName",ScaneManager.GetActiveScene().name);
and when you click the restart button in looseScene then call some method like this,
public void RestartSameLevel(){
SceneManager.LoadScene(PlayerPrefs.GetString("LastLevelName","DefaultSceneName");
//"DefaultSceneName" can be your StartScene name
}
You can restart the scene
Application.LoadLevel(Application.loadedLevel)
Or
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);

How to show cursor in Unity "Play mode"

I am new to Unity, and have created a simple start menu with a play button that will load a game scene. However, when I click "Play" in Unity to test my game, I do not have a cursor. I have to hit escape for my cursor to appear. Do I need to create a script file to make my cursor visible, or how do I change this? I am unable to find anything in Unity forums. Googling the topic retrieves many "How to hide cursor" threads.
I have tried adding an empty object and adding a c# script to it that contains the following but I think I am missing a step?
public class CursorBehavior : MonoBehaviour {
// Use this for initialization
void Start () {
Cursor.visible=true;
}
}
I think this can help you.
Cursor.visible

Play animation once?

I've made a simple animation for my dishwashing machine in 3dsmax. I imported the object as an fbx file into Unity. The animation is called "openandclose". The animation itself opens and closes the machine.
However I can't seem to find a way to trigger the animation to happen once at the click of a button. How can I do this?
Unity has a great documentation regarding every core function including animations. Check this link out: http://docs.unity3d.com/462/Documentation/ScriptReference/Animation.Play.html
and to get the button pressed:
http://docs.unity3d.com/462/Documentation/ScriptReference/Input.GetKeyDown.html
combined it would be something like this i guess:
void Update() {
if (Input.GetKeyDown("e"))
animation.Play("openandclose");
}

Categories

Resources