how do I hide a button on unity - unity3d

I want to create a button that when you press it, a new button will open. I know how to make both buttons, but I can't manage to hide the first button once clicked.
This is my code so far:
#pragma strict
function Start ()
{
}
function Update ()
{
}
var isButtonVisible : boolean = true;
var buttonRectangle : Rect = Rect(100, 100, 100, 50);
function OnGUI ()
{
var NewButton = GUI.Button(Rect (Screen.width / 2 - 75, Screen.height / 2 -25,150,50), "this is also a button");
if ( isButtonVisible )
{
if ( GUI.Button(Rect (Screen.width / 2 - 75, Screen.height / 2 -25,150,50), "button") )
{
isButtonVisible = false;
if ( isButtonVisible )
{
return NewButton;
}
}
}
}
I am new to programing so this question might be a bit unclear.

I agree with "Happy Apple's" solution, and just to expand on this if you wanted to integrate reverse functionality you can simply alter the code as follows:
var isButtonVisible : boolean = true;
var buttonRectangle : Rect = Rect(100, 100, 100, 50);
function OnGUI ()
{
if(isButtonVisible)
{
if(GUI.Button(Rect(Screen.width/2 - 75,Screen.height/2 - 25,150,50),"button"))
{
isButtonVisible = false;
}
}
else
{
if(GUI.Button(Rect(Screen.width/2 - 75,Screen.height/2 -25,150,50),"this is also a button"))
{
isButtonVisible = true;
}
}
}
Hope this is helpful.

It was just a logic error. Firstly you're checking if ( isButtonVisible ) inside another if ( isButtonVisible ) bracket, which is redundant. Secondly if we know the condition we want for the second button to appear (first button being clicked) and the boolean flag for said button being clicked (isButtonVisible == false) we can just branch the isButtonVisible condition to display the second button when this is false.
Assuming you wanted a first button to make another one appear and hide itself on click, this should do what you wanted (though it will only logically flow one way, that is the first button will hide itself and show the second button, but not reversable). So your original code was pretty close.
var isButtonVisible : boolean = true;
var buttonRectangle : Rect = Rect(100, 100, 100, 50);
function OnGUI ()
{
if ( isButtonVisible )
{
if ( GUI.Button(Rect (Screen.width / 2 - 125, Screen.height / 2 -175,150,50), "button") )
{
isButtonVisible = false;
}
}
else
{
var NewButton = GUI.Button(Rect (Screen.width / 2 - 75, Screen.height / 2 -25,150,50), "this is also a button");
}
}
Admittedly there's several better ways to implement this but I hope it solved your problem.

Related

How to I transition from "Start Game" page to playing the actual game?

var start = true; // the game will begin on the "start" screen
var play = false;
var gameOver = false;
clickX = 0; // track the mouse click location
clickY = 0;
function setup() {
createCanvas(640, 480);
}
function draw() {
//Leave this draw() function alone
if (start){
startScreen();
}
else if (play){
playScreen();
}
else if (gameOver){
gameOverScreen();
}
}
function mousePressed(){
clickX = mouseX; // grab the X location of the mouse
clickY = mouseY; // grab the Y location of the mouse
}
function startScreen(){
background(50);
textSize(20);
fill(255, 0, 0);
text('To begin playing, click mouse',60,80);
if(dist(clickX, clickY, width/2, height/2) < 100){
}
}
function playScreen(){
}
function gameOverScreen(){
}
My teacher said "add some branching logic to the mousePressed() function so that mouse clicks will change the canvas from one screen to the next. Think about how to use if() and else if() statements and those boolean "flag" variables at the top of the sketch (8 points)."
I am trying to create code that will switch the starting screen to the actual game! I'd like to do it so that when you click your
I've been looking everywhere and can't find anything helpful :( So if you have any references I can check out, I will take those too! Thank you!
to manage that state I would recommend you a switch statemen where you can iterate it using a variable with the scenario name:
let scenario ; // the game will begin on the "start" screen and will change to play or gameOver according the needs
function setup() {
createCanvas(640, 480);
scenario = 'playing'
}
function draw() {
startScreen()
text(scenario,width/2,height /2);
}
function mousePressed(){
// Here you can add different check according to each scenario
switch(scenario){
case 'start':
if(dist(mouseX, mouseY, 60, 80) < 100){
scenario = 'playing'
}
break;
case 'playing':
if(dist(mouseX, mouseY, 60, 80) < 100){
scenario = 'gameOver'
}
break;
case 'gameOver':
if(dist(mouseX, mouseY, 60, 80) < 100){
scenario = 'start'
}
break;
}
}
function startScreen(){
switch(scenario){
case 'start':
background(50);
textSize(20);
fill(255, 0, 0);
text('To begin playing, click mouse',60,80);
break;
case 'playing':
background(200,0,0);
textSize(20);
fill(255, 0, 0);
text('Click to continue to GameOver',60,80);
break;
case 'gameOver':
background(0,255,0);
textSize(20);
fill(255, 0, 0);
text('Click to go to start',60,80);
break;
}
}

How to create a Phaser button in Facebook instant games for a mobile device

I am trying to create a button with Phaser 2 CE on a mobile device but it doesn't fires, even if it works fine on desktop, the entiere code will be on my github repository but for the moment i can just display an image like in the code bellow
var game = new Phaser.Game(640, 480, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
function preload () {
game.load.image('logo', 'assets/phaser2.png');
game.load.image("upArrow", "assets/up.png");
}
function create () {
// Initialize player
//player = game.add.sprite(20, 20, 'logo');
game.device.desktop || addMobileInputs();
}
function addMobileInputs() {
upButton = game.add.sprite(40, 40, "upArrow");
upButton.inputEnabled = !0;
upButton.events.onInputDown.add(myHandler, this);
}
function myHandler() {
alert("up");
}
function update() {
}
It seems that alert("...") doesn't fire, and the code is correct because if you just make your sprite move, it moves, for example like the code below:
var game = new Phaser.Game(640, 480, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
var sprite;
function preload () {
// This is equivalent to <https://examples.phaser.io/assets/>.
this.load.image('dude', 'assets/sprites/phaser-dude.png');
}
function create() {
sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'dude');
sprite.inputEnabled = true;
sprite.events.onInputDown.add(myHandler, this);
}
function myHandler() {
sprite.x += 10;
}
function update() {
}

Show the name of hit gameObject

I'm creating a project for my school and it was going pretty good until now. I've searched for an answer for nearly an hour and still couldn't find anything (wrong tags?).
The thing is that I want to show the name of the item that player is hitting with raycast. I tried this but sadly it's not working:
#pragma strict
var rayLength : int = 10;
private var inventory : Inventory;
private var guiShow : boolean = false;
var bush : GameObject;
var player : GameObject;
function Start()
{
inventory = GameObject.Find("First Person Controller").GetComponent(Inventory);
}
function Update()
{
var hit : RaycastHit;
var forward = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, forward, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Wood")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
inventory.wood++;
Destroy(hit.collider.gameObject);
guiShow = false;
}
}
else if(hit.collider.gameObject.tag == "Sticks")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
inventory.stick++;
Destroy(hit.collider.gameObject);
guiShow = false;
}
}
else if(hit.collider.gameObject.tag == "BushFull")
{
guiShow = true;
bush = (hit.collider.gameObject);
if(Input.GetKeyDown("e"))
{
inventory.berry += 5;
bush.GetComponent(BushController).berriesTaken = true;
guiShow = false;
}
}
else
{
guiShow = false;
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Pick up" + hit.collider.gameObject);
}
}
If anyone knows answer - please help me. Thanks in advance!
Just use hit.collider.name to retrieve the gameObject's name. If that doesn't work (which I'm 99% sure it will), use hit.collider.gameObject.name
Your code is a little tricky because maybe it would be a greater idea not to use the OnGUI() method. It's easier to call a custom method from update when the raycast hit the player.
One example of an easier implementation is the following:
function Update()
{
var hit : RaycastHit;
var forward = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, forward, hit, rayLength))
{
Debug.Log (hit.collider.gameObject.name); //Shows the hittenGo in the console
drawColliderName(hit.collider.gameObject.name);
if (hit.collider.gameObject.tag == "Woods")
{
//do Woods tuff
}
else if (hit.collider.gameObject.tag == "Sticks")
{
//do Sticks stuff
}
else if (hit.collider.gameObject.tag == "BushFull")
{
//do BushFull stuff
}
}
}
Of course you have to implement the method drawColliderName where you must draw the Collider name on the GUI.
If you just want your code to work, in the OnGUI() you have to call name variable, like in the Debug.Log call of my code, this is just calling hit.collider.gameObject.name instead of hit.collider.gameObject

How to pick up an object using a GUITexture

How can I implement a GUITexture for picking up an object?
The problem is I have a GetButtonUp input, how can I change it to GUI Button?
The use is a layer type of my tag type. Here is the code:
#pragma strict
var Primary : Item; //Your Primary item holder
var Secondary : Item; //Your Secondary item holder
var myLayerMask : LayerMask;
private var HitItem : GameObject; //Item that raycast hits.
function Update ()
{
if(Input.GetButtonUp("Use")) //Remember to make new Input called "Use" :)
{
var hit: RaycastHit;
var Direction = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, Direction * 2.5, Color.blue); //Shows raycast when you press "Use"
if(Physics.Raycast(transform.position, Direction, hit, 2.5, myLayerMask))
{
if(hit.transform.tag == "Item") //If raycast hits transform.tag "Item".
{
HitItem = hit.transform.gameObject; //HitItem becomes hit.gameObject.
var ItemScr : ItemScript = HitItem.GetComponent("ItemScript");
if(ItemScr.MyItem.IsSecondary) //If Hit item is Secondary
{
if(Secondary.ActiveObj == null) //Checks if you dont have Secondary Item
{
Secondary = ItemScr.MyItem;
Destroy(hit.transform.gameObject);
if(Primary.ActiveObj == null) //Checks if you have already
{ //equipped item in your hand.
Secondary.ActiveObj.active = true;
}
else if(Primary.ActiveObj)
{
if(Primary.ActiveObj.active == false)
{
Secondary.ActiveObj.active = true;
}
}
}
}
//And same to primary :)
if(!ItemScr.MyItem.IsSecondary) //If Hit item is Primary
{
if(Primary.ActiveObj == null) //If you don't have primary, goes on...
{
Primary = ItemScr.MyItem;
Destroy(hit.transform.gameObject);
if(Secondary.ActiveObj == null)
{
Primary.ActiveObj.active = true;
}
else if(Secondary.ActiveObj)
{
if(Secondary.ActiveObj.active == false)
{
Primary.ActiveObj.active = true;
}
}
}
}
}
}
}
}
Any call with GUI needs to be called in the OnGUI() method. And usually GUI scripts are put separate from other parts of your program and attached to the main camera or an empty object.
-Personally I use main camera.
Unity had a good walk through Here
And if you want to change to c# i suggest google, which I believe should be a perfect source for this type of question.

this is my popuppanel's occurence code.....but it only works 1 time

popup.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
int r = getToothNumber();
if(r == 14 || r == 15 || r == 16) {
popup.setPopupPosition(event.getClientX() - 170, event.getClientY());
popup.show();
} else if(r > 16) {
popup.setPopupPosition(event.getClientX() - 56, event.getClientY() - 550);
popup.show();
} else {
popup.setPopupPosition(event.getClientX() + 22, event.getClientY());
popup.show();
}
}
});
Your code should always show a popup. The problem is probably not in the code you are showing. My guess is that you are not simply hiding popup. You might be destroying it, in which case a show() will not work. Please try to always create a new popup and not reuse it.