Unity: Only mouse works on first input (EventSystem / Canvas) - c#

In Unity I have a simple world space canvas with a single button set up.
If you click the button with the mouse it works.
If you use another method (Like 'joystick button 0') it will not activate the button.
Once 'mouse button 0' has been clicked once subsequent button clicks using the 'joystick button 0' register.
Additional info; If you set the project to VR or move cursor away from the button 'joystick button 0' will continue to activate the button even if the cursor isn't over the button. It will stop once 'mouse button 0' is pressed to deselect the button.
The goal is to use 'joystick button 0' / 'joystick button 1' as the only inputs to the canvas and ultimately have a mouse look feature using a joypad.
Any Idea what is going on / how to get this working?
Input is the default unity setup. ('mouse 0' only mapped to 'Fire1' / not mapped to 'Submit') (EventSystem's "Submit Button" is mapped to 'Submit')
See Script:
using UnityEngine;
using System.Collections;
public class ButtonTestScript : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Submit"))
Debug.Log("Submit");
else if(Input.GetButtonDown("Cancel"))
Debug.Log("Cancel");
//Lock The Cusor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = true;
}
public void ButtonClick()
{
Debug.Log("Button Clicked = " + gameObject.name);
}
}

The short answer is the system isn't built to work like that. The longer answer is:
https://unity3d.com/learn/tutorials/topics/virtual-reality/interaction-vr

Related

my button doesnt trigger as should and instead gives two errors (before and after click)

hello I have a button plus a box which alpha is 0 from the start, the button works but the box doesn't appear. In the code the I did use 'getcontents' but it seems it didn't work, the button executes a debug.log so that's how I know that is working, the errors I get before the button click is;
before button click
and the errors after button click (including the debug.log)
after button click
it says i haven't attached my 'Menuee' although i have (yes i know i spelt it like that)
show here;
attached Menuee
and i have attached my Ibutton (the button)
my code is below;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class startbutton : MonoBehaviour {
public Button Ibutton;
public CanvasGroup Menuee;
public void Start () {
Ibutton.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick(){
Debug.Log ("yes");
Menuee.interactable= true;
Menuee.alpha = 1;
}
}
In the screen shot you don't seem to have anything assigned to Button Ibutton.
Another reason for such errors could be that you might have assigned the startbutton script to another game object somewhere and that instance of the script doesn't have the properties assigned.
You didn't add a button on your public Button Ibutton;
You might also some how delete EventManager that comes with canvas you created on hierarch window.

Getting the menu button input on the SteamVR controller

Hi2,
Does anyone know how to get the menu button input on the steamVR controller via C# code in Unity?
Currently I am able to get the input from the the trigger, trackpad, and grip button.
private void Update()
{
if (SteamVR_Input._default.inActions.GrabGrip.GetStateDown(inputSource))
Debug.Log("grab grip"); // the side button on the controller
if (SteamVR_Input._default.inActions.GrabPinch.GetStateDown(inputSource))
Debug.Log("grab pinch"); // the back button on the controller
if (SteamVR_Input._default.inActions.Teleport.GetStateDown(inputSource))
Debug.Log("teleport"); // the big middle button on the controller
}
any help is appreciated. ^_^
Select in the menu 'Window -> SteamVR Input'. From there click on 'Open Binding UI'.
Your browser will open a screen where you can see this:
The red arrow points to where you can add an action for the menu button (sorry, mine is in German). Call it 'MenuClick' and make sure you save that in your private settings.
Then, in your code access it as
if (SteamVR_Input._default.inActions.MenuClick.GetStateDown(inputSource))
Debug.Log("menu button pressed"); // the menu button on the controller

How do you check if a button component is present in UI in Unity?

I am new to Unity and was wondering if it was possible to check if a button component in present in the scene, this is like a UI test.
I am trying to do this using the Unity UI Test Automation tool.
While zyonneo answer is correct, GameObject.Find can be slow if there is a lot of gameObjects.
What I would do instead is looking for the button component in the canvas children. For this you need a reference to the parent canvas of your button.
public GameObject MyCanvas;
void Start(){
if(MyCanvas.GetComponentInChildren<Button>() != null){
Debug.Log("Button found");
else {
Debug.Log("Button not found");
}
}
If you want more information about what is the most efficient way to find a gameObject, I recommend you to read this thread on Unity Formus.
Add the code where you want to check whether the button is present in Hierarchy.If you have added the button in the canvas you can find using the below code.Just type the exact name of the button.
By default when you add a button it will have the name "Button" so search with that parameter.Here I am adding a button by right clicking and then renaming it into "Btn01"
if(GameObject.Find("Btn01")!= null)
{
Debug.Log("Button is Present in Hierarchy");
}
else
{
Debug.Log("Button Not Present");
}

Windows Mixed Reality - AttachToController

So I'm using the AttachToController script to attach a window floated on top of the controller - which works great. In the script that calls up the window, I figure out which hand pressed the controller's menu button and set the Handedness field appropriately (Left or Right). The problem I'm trying to solve is this: Let's say the user clicks on the right controller's menu button and then later on, the left menu button is clicked. The problem I'm experiencing is that even though I've altered the Handedness field, the window still appears attached to the right controller.
private void InteractionManager_InteractionSourcePressed(InteractionSourcePressedEventArgs args)
{
hand = args.state.source.handedness;
...
}
private void SetHandednessAndActivate(GameObject go)
{
AttachToController script = go.GetComponentInChildren<AttachToController>();
if (script != null)
{
script.Handedness = hand;
}
go.SetActive(true);
}
Just to be clear, if the user clicks the left controller menu button first, the window is always on the left and the same holds true for the right controller. What I want is for the window to move to whichever controller is used.
Instead of
script.Handedness = hand;
Use
script.ChangeHandedness(hand);
All the other bits are handled by the script.

Problems with canvas buttons in unity

So, i have buttons for my game that include : Main Menu button , Restart button and Play button. Everything works fine, but occasionally one of the button's text doesn't load , one of them doesn't work. I'm not sure how to fix this, because it happens randomly and not every time.
The code for the buttons :
GameObject[] pauseObjects;
void Start()
{
Time.timeScale = 1;
pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");
hidePaused();
}
public void showPaused()
{
foreach (GameObject g in pauseObjects)
{
g.SetActive(true);
}
}
public void hidePaused()
{
foreach (GameObject g in pauseObjects)
{
g.SetActive(false);
}
}
public void LoadLevel(string level)
{
Application.LoadLevel(level);
}
public void pauseControl()
{
if (Time.timeScale == 1)
{
Time.timeScale = 0;
showPaused();
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1;
hidePaused();
}
}
The way your UI is setup is not right. If you have to loop through everything during pause, you are doing it wrong. I have answered a similar question before and will just copy and paste this here.
Use different Canvas to separate your UI and group them depending on when they are displayed. Then you can toggle the whole Canvas on/off. Your [post][1] before this post shows that you are using image as button and you have a different script attached to two images you are using as buttons. Use Buttons for buttons and image for non-clickable objects. Simple as that. So change those images to buttons.
You don't need the GameOver scene. A GameOverCanvas is fine.
The GameObjects in bold are the parent Objects(Canvas). The ones under that starts with '-' are the child GameObjects.
Step 1:
Create Canvas and name it MainMenuCanvas (First UI to display when game Loads).Create each child button and rename them as below(GameObject->UI->Button):
-playButton;
-settingsButton;
-exitGameButton;
Attach the MainMenuCanvas script to the MainMenuCanvas Object.
Step 2:
Create a Canvas and name it GameCanvas (Displayed during game).Create each child button and rename them as below(GameObject->UI->Button):
-pauseButton
-jumpButton
Attach the GameCanvas script to the GameCanvas Object.
Step 3:
Create a Canvas and name it PauseCanvas (Displayed when pause button is clicked).Create each child button and rename them as below(GameObject->UI->Button):
-resumeButton;
-backToMainMenuButton;
-settingsButton;
-exitGameButton;
Attach the PauseCanvas script to the PauseCanvas Object.
Step 4:
Create a Canvas and name it SettingsCanvas (Displayed when settings button is clicked).Create each child button and rename them as below(GameObject->UI->Button):
-backButton;
Attach the SettingsCanvas script to the SettingsCanvas Object.
Step 5:
Create a Canvas and name it GameOverCanvas (Displayed when Game is Over or player is killed).Create each child button and rename them as below(GameObject->UI->Button):
-playAgainButton;
-backToMainMenuButton;
-exitGameButton;
Attach the GameOverCanvas script to the GameOverCanvas Object.
Step 6:
In the Game scene, make sure that only GameCanvas is enabled. The rest of the canvas should be manually disabled.
Step 7:
In the Menu scene, make sure that only MainMenuCanvas is enabled. The rest of the canvas should be manually disabled.
Once you get this setup correctly, the UI code templates I provided should work. No more UI overlapping or text disappearing. You can easily add or remove features.
Your UI setup should look like the image below.

Categories