Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by shads11858 · Sep 10, 2013 at 03:57 PM · c#guibuttonsgui-button

GUI Buttons not Being Displayed

I've been going over this problem for last hour and yet to figure out why my buttons aren't coming up and hopefully someone out there can see something obvious I'm missing.

I have other buttons in my script that all work fine. E.g. I have a Input.GetMouseButtonDown(1) event that is triggered and sets a bool value sowSelect to true and the next piece of code in my OnGUI().

         if(sowSelect){
             //if statement runs if button clicked, displays wheat image and paints selected tile with type 2
             if (GUI.Button(new Rect(button.x, button.y, 50, 50), wheatButton, blankbg)){    
                 if(randomTilePainter){ // if true paint 5 tile type
                     //check to see if this tile has a crop object assigned to it
                     if(tile.assignedCrop == null){
                         tile.assignedCrop = new Wheat(Time.time);
                         terrain.PaintTile1x1(tile, 5); //asigned it a seedling image.
                         moneyCoutner.GetComponent<MoneyDisplay>().takeFunds(tile.assignedCrop.getCost()); // take whatever the cost is from the object
                     }//end assigned crop
                     randomTilePainter = false; //swap tile painter
                 }else if(!randomTilePainter){//if false paint 6 tile type.
                     //check to see if this tile has a crop object assigned to it
                     if(tile.assignedCrop == null){
                         tile.assignedCrop = new Wheat(Time.time);
                         terrain.PaintTile1x1(tile, 6);    
                         moneyCoutner.GetComponent<MoneyDisplay>().takeFunds(tile.assignedCrop.getCost());
                     }//end tile null.
                 }//end randomtile if
                 sowSelect = false;            
             }//end wheatbutton if

Now this code works fine and buttons display on click, but on virtually the same code that on mouse down sets bool value harvestAble to true the follow code happens:

             //if harvestable has been set, show buttons.
             if(harvestAble){
                 //check what type is ready for harvest - SWITCH may be better by giving each crop an id, 0 wheat, 1 corn etc.
                 if(tile.assignedCrop.getType() == "Wheat"){
                     print("tiel if" + tile.assignedCrop.getType());
                     //show button for wheat harvest
                     if(GUI.Button(new Rect(button.x, button.y, 50, 50), wheatHarvestButton, blankbg)){    
                         tile.assignedCrop.harvest(Time.time);
                     }//end wheatbutton if
                 }else if(tile.assignedCrop.getType() == "Corn"){ //if it is corn
                     print("tile if" + tile.assignedCrop.getType());
                     //show corn harvest button
                     if(GUI.Button(new Rect(button.x, button.y, 50, 50), cornHarvestButton, blankbg)){    
                         tile.assignedCrop.harvest(Time.time);
                     }//end wheatbutton if
                 //else if users has already selected harvest for this grid, show selected already so not to reharvest.
                 }else if(tile.assignedCrop.getHarvesting()){
                     if(GUI.Button(new Rect(button.x, button.y, 50, 50), "This crop is already being harvested!")){    
                     
                     }//end harvested button if    
                 }//end gettype if
                 harvestAble = false; // Stop it trying to reharvest item 
             }//end harvestable if

Now the print functions I put in the if statement to check if the mouse click event is activated and goes into code just before the button if and it displays that tile if and also lists that it does have a type and it is correct, but no button. I've even copied by button code from the sowSelect which worked and still nothing. I've even set the new Rect(250,250,50,50) just to see if stating where to place it helped, but noavail and I'm out of ideas so if anyone can help I'd be in your debt.

Comment
Add comment · Show 2
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 PAEvenson · Sep 10, 2013 at 05:25 PM 0
Share

Are those print functions flooding the console window? $$anonymous$$y guess is your problem is where you handle the input. If youre only seeing 1 print, comment out this line and see if the button comes up:

  //harvestAble = false; // Stop it trying to reharvest item 
avatar image Jamora · Sep 10, 2013 at 06:40 PM 0
Share

Because I think PAEvenson is correct in his suggestion, I will ins$$anonymous$$d suggest you use an IHarvestable interface ins$$anonymous$$d of checking each type of plant. You should be able to shorten your second snippet to this, regardless of how many different plant types you have.

 if(harvestAble){
     //check what type is ready for harvest.
     if(tile.assignedCrop.getType() == "IHarvestable"){
         print("tiel if" + tile.assignedCrop.getType());
         //show button for harvest
         if(GUI.Button(new Rect(button.x, button.y, 50, 50), tile.assignedCrop.GetHarvestButton(), blankbg)){
             tile.assignedCrop.harvest(Time.time);
                     harvestAble = false; // I think this belongs here.
         }//end harvestableButton if
          //else if users has already selected harvest for this grid, show selected already so not to reharvest.
     }else if(tile.assignedCrop.getHarvesting()){
         if(GUI.Button(new Rect(button.x, button.y, 50, 50), "This crop is already being harvested!")){
                 harvestAble = false; // possibly here too.
         }//end harvested button if
     }//end gettype if
 }//end harvestable if

All you need is to make an interface that contains all the methods anything harvestable needs: based on your snippet, it would contain at least the following:

 public interface IHarvestable{
     string GetHarvestButton();
     bool getHarvesting();
 }

You'd use the interface like this, in each of your plant classes:

 public class Wheat : IHarvestable{
     public string GetHarvestButton(){/*implementation for Wheat*/}
     public bool getHarvesting(){/*implementation for Wheat*/}
 }

If you have a concrete or abstract base class, remember to put it before any interfaces. After you've added the interface, you can use any instance of Wheat as either an instance of IHarvestable or Wheat, whichever suits your needs.

1 Reply

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

Answer by whydoidoit · Sep 10, 2013 at 07:39 PM

OnGUI is called every frame (many times), so every frame that you want the buttons to be visible you must pass through the code.

Your sowSelect function sets sowSelect to false when the wheat button is pressed, but your harvestable code sets it to false inside the "if harvestable" block - this means that it will only ever be displayed for a single frame - moreover, OnGUI is called multiple times per frame, so the button actually only exists for the layout pass, after which the if harvestable will be false again, so it is actually never drawn.

Basically don't set harvestable to false until the user does something.

Comment
Add comment · Show 2 · 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 shads11858 · Sep 11, 2013 at 12:40 AM 0
Share

Thanks, I can't believe I missed that. I think I had a similar problem when I was implementing the sowSelect if statement and eventually realized my setting of the true at end of the if meant it would disappear straight away. Thanks for the posts!

avatar image alonsoGarrote · Sep 16, 2013 at 06:05 PM 0
Share

Hello there, $$anonymous$$y GUIbuttons arent being displayed either, Im trying but cant get it solved. Can you please take a look at it?, Its for a college project and Im stuck on this... link text

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

19 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

Related Questions

How do I make a custom font for a GUI button? C# 1 Answer

Display Gui Button On 3d Object location 1 Answer

Multiple Cars not working 1 Answer

On Screen Button Controls 0 Answers

Problem with rect and touch 1 Answer


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