Search Unity

How to make a UI button click show a gameobject in front of player??

Discussion in 'VR' started by KenniKure, Sep 16, 2019.

  1. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    Hi Forum

    I have tried a few codes/scripts but nothing has been working so far. The idea is pretty simple. I have a UI canvas with two buttons on. The "Quit" button turns red when I hover it and click it, and when clicked the menu and the gazepointer/gaze ring disappears. (as intended)

    The problem is, that when I hover and click the other button nothing happens. The idea is, that when I click it, it should display/open a gameobject in front of the player (in VR). I have tried dragging the gameobject to the OnClick event on the button and put the SetActive function on it but it doesnt work?

    Can anyone help me and tell me how to do this? :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well since one button works, it appears that your event/UI system is correctly set up in general. So something's wrong with this particular button, or with how you've hooked it up. What you're trying to do doesn't require any code; dragging the GameObject to the OnClick event and having it invoke SetActive(true) is the right thing to do.
     
  3. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    I got it working now :) The only problem is that the gameobject appears at a fixed position. I have an idea on how to get it to appear in front of me, but I dont know how or where to implement it in my code as it is?
    upload_2019-9-17_8-1-46.png
    I dont know if I can make an if stament inside the one I already have? If I want it to appear in front of me I think the code need to be something like "Instantiate(PanelMarkup,transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity)" ??

    And also a float like "distance from player" on for example 2f??
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hmm. Well calling Instantiate would make a new object. It sounds to me like you just want to show a previously-hidden object. So don't call Instantiate.

    Instead, you might use the OnEnable method to position the object. Perhaps something like this:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PositionInFront : MonoBehaviour {
    4.    
    5.     public float distance = 2f;
    6.    
    7.     protected void OnEnable() {
    8.         Transform cam = Camera.main.transform;
    9.         transform.position = cam.position + cam.forward * distance;
    10.     }
    11. }
     
  5. Corysia

    Corysia

    Joined:
    Mar 14, 2018
    Posts:
    108
    JoeStrout likes this.
  6. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    Yeah its kinda like the other topic, but I have made a few other things work and found other solutions since then. I want it to stay with me if I move :)
     
  7. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    That did the trick :) Thank You!!!