Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MrCrumbl3d · Apr 08, 2017 at 06:12 AM · uibuttonsif-statementsif-else

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 () {



 }
     

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Namey5 · Apr 08, 2017 at 07:41 AM 0
Share

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.

avatar image MrCrumbl3d · Apr 08, 2017 at 08:32 AM 0
Share

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();
          }
      }
avatar image hexagonius · Apr 08, 2017 at 01:45 PM 0
Share

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

avatar image MrCrumbl3d · Apr 08, 2017 at 04:34 PM 0
Share

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...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrCrumbl3d · Apr 09, 2017 at 05:36 AM 0
Share

So i will change my void update to void ongui?..

And also thanks for your answer.

avatar image OfficialCoatsee MrCrumbl3d · Apr 09, 2017 at 05:56 AM 0
Share

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.

avatar image MrCrumbl3d · Apr 09, 2017 at 06:02 AM 0
Share

Thank you that's what i want..

avatar image OfficialCoatsee MrCrumbl3d · Apr 09, 2017 at 06:05 AM 0
Share

Glad I could help. Good luck.

avatar image MrCrumbl3d OfficialCoatsee · Apr 09, 2017 at 07:15 AM 0
Share

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;

         }

     }

 }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges