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

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? :slight_smile:

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.

I got it working now :slight_smile: 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?
4970873--484142--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??

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:

using UnityEngine;

public class PositionInFront : MonoBehaviour {
   
    public float distance = 2f;
   
    protected void OnEnable() {
        Transform cam = Camera.main.transform;
        transform.position = cam.position + cam.forward * distance;
    }
}

Deja vu.

Isn’t this exactly the same thing? Whether it’s UI or an object, it’s a game object appearing before you.

Is it that you’re wanting it to be attached to you, so as you move it stays with you or that you just want it to instantiate in front of you?

1 Like

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 :slight_smile:

That did the trick :slight_smile: Thank You!!!