- Home /
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.
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
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.
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.
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!
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
Follow this Question
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