I can't assign scripts to buttons in UNITY? - c#

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

Related

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

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).

Steam VR Scene stops responding on reload in Unity

I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:

In Unity, when I load a scene a second time, some objects don't show up

I have two scenes: Menu and Game. I'm using C#.
When you die in the game, you get sent back to the menu with
SceneManager.LoadScene("Menu");
There's a script with a GUIButton in the menu that, when clicked, loads the game with
SceneManager.LoadScene("Game");
What I want to happen is that when I click the button in the menu scene, it loads the game as if I just clicked the play button with the game scene open in the editor.
What instead happens is that it goes to the game scene, but some objects from the game scene appear to be missing. I'm not using DontDestroyOnLoad() anywhere.
Some objects from the scene do appear, but others don't. The weirdest thing is that there are some data fields on a script on the missing object that are referred to by some other scripts, and those give values that make sense.
Does anyone have any idea what's going on, or what I can do to get the desired result?
Turns out that the problem was that I was destroying one of my objects if it was not the first time that object was created, and I didn't realize it.
are those static objects or dynamic? try to instantiate them in function which gets called on scene loaded.

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

How to initiate click to activate code in Unity 3D?

I am designing a shooting game in Unity 3d for which I have a requirement that only when an image is tapped the shooting should occur. Please check the command used for the purpose:
if (Input.GetMouseButtonDown(0)){
if (!clickedConfirmed){
GameObjectClicked = GetClickedGameObject();
clickedConfirmed = true;
}
Currently, shooting occurs when clicked anywhere on the screen. How can activation of shooting be bonded only to the gameobject (image) instead of being activated when clicked anywhere on the screen.
Your need to give more context in your example, especially what GetClickedGameObject() is actually doing internally.
As such I'm limited in what I can say is the problem, but I suspect that you aren't using GameObjectClicked correctly.
I would expect that you'd do a check such as:
this.gameObject == GameObjectClicked
Note that if this being used as a method of developing a UI there are more elegant solutions out there, including the long awaited new GUI system included in the Unity3d 4.6 open beta.

Categories

Resources