ui button

hi all.
I’m a newbie. a week can not solve the problem, looked a lot of forums.
I have a character and a cube. cube is removed when a character enters the cube. I added a UI BUTTON that would be carried out to check if the character and pressing the UI Button. but the cube does not respond to button ((I feel that the decision to close but my knowledge is still too little to understand

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class destroy : MonoBehaviour {
public GameObject GameObj;
public Button Mine;
// Update is called once per frame
void OnTriggerEnter (Collider col)
{
Button btn = Mine.GetComponent ();
if (col.tag == “Player”)
if (btn.onClick == true)
{
Destroy (GameObj);
}
}
}
hat is not correct in pressing the check?

First I’ll suggest Using code tags properly - Unity Engine - Unity Discussions
Code tags are very handy.

Second, your script isn’t going to work. There are a few issues. OnTriggerEnter happens when a rigidbody enters into the collider that has a trigger on it, or if it has a rigidbody on it, you only need a collider to hit the trigger. It triggers once, not multiple times. It doesn’t check to see if you are standing in the trigger when you hit a button.

Next, your line
Button btn = Mine.GetComponent() isn’t really needed. If you have the button dragged into your public Button Mine variable in the inspector, there is no need to create another reference to it.

So, what do you need to do?
You’ll need to have a bool for when you step into your trigger, it’s set to true. And probably when you exit, you set to false(unless that’s not the desired effect).

Then, you need a method that triggers when the button is hit. This can be tied into the button in the inspector or added to the button through script. When the button is hit, the code will check your bool to see if it’s true and then destroy the cube.

You can also look into OnTriggerStay Unity - Scripting API: MonoBehaviour.OnTriggerStay(Collider)
But honestly, I think the above method is probably going to be a better choice.

thanks for tags and I 1 times in the forum and have not found how to make the code as other people
maybe I’m wrong to express my thoughts
I can not figure out how to verify that the button = true
because when object in trigger , the cube will not be removed until the button is still true
in any case, thank you

What is it you are trying to do with button? I may not understand it. But if you step into a trigger collider and have a gui button appear and then want the player to click on it, that button needs to be tied to something for the onClick. This can be tied in by the inspector or by code using onclick.addlistener.

Honestly, if your button is only going to appear when a person steps into the collider, you can have the trigger set your button gameobject active when they step in or turn it off when they step out. But this way, the button is only up when you have stepped into the trigger. Then when the button is clicked, the cube is destroyed and the button should go away.

There is a button on the screen constantly.
I just wish that command at the destruction have not been performed, even when the collider is located in the trigger until I will click button

If the button is there constantly, then you’re going to have to go with the bool method. Set the bool to true and then hit the button. You’ll need the button to tie to code in it’s onclick. Then you’ll need to have something to indicate what cube you are going to destroy.