HELP ME PLEASE TO FIX MY PROBLEM..
Help.. how to use if statement in ui buttons... inside of private void update... Im trying to make a ui buttons inside of the if statement... but is always show in the console edit - projects settings - inputmanage blahblah.. but i already made my own function when i hit the button... in the void start i create some onclick listener and when the player hit the button i made my function to and that's is void pickUpdown and placeDown. or there's even wrong with GetButtonDown?
If you dont understand... try my other question.
I made my ui buttons and my platform is android. This is what i want when the raycast hit in certain distance.. then the ui buttons can do his function that i made and that's is (void pickUpDown) and (void placeDown) and how do i do that with if statement. I already add some onClick and add listener in void start. So i hope you understand sorry...
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class rayCastCSHARP : MonoBehaviour {
public GameObject _parent;
public GameObject _testObj;
public GameObject _parent1;
public Button pickUp;
public Button place;
void Start () {
pickUp.onClick.AddListener(pickUpDown);
place.onClick.AddListener(placeDown);
}
private void Update()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit = new RaycastHit();
if (Physics.Raycast(transform.position, fwd, out hit))
{
if (hit.distance <= 1 && hit.collider.gameObject.tag == "pickup")
{
if(Input.GetButtonDown("pickUp")) {
pickUpDown();
}
}
}
if (Input.GetKeyDown("space"))
{
_testObj.transform.parent = _parent1.transform;
print("please lord");
}
}
void pickUpDown () {
}
void placeDown () {
}
}
What exactly is the problem? It looks like you're using if statements. Is there a particular reason the Update function has to be private? Please provide more information if you want help.
Im trying to make a ui buttons inside of the if statement... but is always show in the console edit - projects settings - inputmanage blahblah.. but i already made my own function when i hit the button... in the void start i create some onclick listener and when the player hit the button i made my function to and that's is void pickUpdown and placeDown. or there's even wrong with GetButtonDown?
if (Physics.Raycast(transform.position, fwd, out hit)) { if (hit.distance <= 1 && hit.collider.gameObject.tag == "pickup") { if(Input.GetButtonDown("pickUp")) {
pickUpDown();
}
}
What do you mean by making a button? You're not instantiating one and buttons don't have anything to do with the Inpiut$$anonymous$$anager, unless you talk about gamepad input
Sorry.... let me try this...
I made my ui buttons and my platform is android. This is what i want when the raycast hit in certain distance.. then the ui buttons can do his function that i made and that's is (void pickUpDown) and (void placeDown) and how do i do that with if statement. I already add some onClick and add listener in void start. So i hope you understand sorry...
Answer by OfficialCoatsee · Apr 09, 2017 at 05:31 AM
You should be creating your buttons in the OnGUI() method...?
void OnGUI() {
if (GUI.Button (new Rect (0, 0, 200, 50), "Pickup")) { //creates a button and listens for it to be clicked.
pickup(); //once clicked.
}
if (GUI.Button (new Rect (0, 50, 200, 50), "Drop")) { //creates a button and listens for it to be clicked.
drop(); //once clicked.
}
}
Using the OnGUI() method is basically the same as using the void Update() function, except it handles the User Interface.
You can make calls to different functions within the script from there, and you can use the OnGUI() with mobile devices.
I hope I have understood what you are trying to do.
So i will change my void update to void ongui?..
And also thanks for your answer.
No no, you still need your void Update().
But change all of the button handling to void OnGUI().
public GameObject _parent;
public GameObject _testObj;
public GameObject _parent1;
public bool show_pickup_button = false;
void Start () {
}
void OnGUI() {
if (show_pickup_button == true) {
if (GUI.Button (new Rect (0, 50, 200, 50), "Pickup")) {
pickUpDown();
show_pickup_button = false;
}
}
}
private void Update()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit = new RaycastHit();
if (Physics.Raycast(transform.position, fwd, out hit))
{
if (hit.distance <= 1 && hit.collider.gameObject.tag == "pickup")
{
show_pickup_button = true;
} else {
show_pickup_button = false;
}
}
if (Input.Get$$anonymous$$eyDown("space"))
{
_testObj.transform.parent = _parent1.transform;
print("please lord");
}
}
void pickUpDown () {
}
void placeDown () {
}
What this will do, is every time the hit.distance is less than or equal to 1 and the hit.collider.gameObject.tag is equal to "pickup" the script will show a button that can be clicked. Once clicked it will initiate the void pickUpDown() function and will also then hide the button.
If the parameters aren't met, the button will also disappear.
Let me know how you go.
Sorry if i ask this again.. im so new in unity.. im so confuse why this happening... when the player hit the distance it will show the buttons it works but i add something when the player is go away from the certain distance or greater than 1.. still the button is still on.. i dont know why but somtimes it will turn off when i move around... sorry.. i try else-if statement but it never works... thanks!!!
private void Update()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit = new RaycastHit();
if (Physics.Raycast(transform.position, fwd, out hit))
{
if (hit.distance <= 1 && hit.collider.gameObject.tag == "pickup")
{
show_pickup_button = true;
show_place_button = true;
}
if(hit.distance >= 1 && hit.collider.gameObject.tag != "pickup")
{
show_pickup_button = false;
show_place_button = false;
}
}
}
Your answer
Follow this Question
Related Questions
EventSystem button not calling OnClick on Build, works running in Editor 1 Answer
Do Buttons dirty the canvas? 3 Answers
Sketchy looking buttons in android 0 Answers
Equip a Weapon? 0 Answers
Instantiate button on runtime 2 Answers