When I pressed button, It is not woking

I am new in Unity programming!!!
I have a window that show two buttons (for example: btn1 and btn2). When I press the btn1, it shows new box, there is no problem. But in my box have also two buttons (yes and no). In my project when I click the button “No”, box will hide and show just my window. But it is not working, please help me!!!

Here is my code:

public bool myBox = false;

void drawMenu5() {
if (GUI.Button (Rect (70, 150, 120, 50), “btn1”)) {
myBox = true;
}
GUI.Button (Rect (70, 150, 120, 50), “btn2”);

if (myBox == true)
{
GUI.Button (Rect(90, 170, 80, 50), “Yes”);
if (GUI.Button (MakeRect (param), “No”)) {
myBox = false;
Debug.Log(“Pressed NO ->”+myBox);
}
}
}

I dont understand what you are trying to do . and that code isnt doing anything but drawing gui boxes and removing boxes

When posting code please use code tags.

in your code btn1 and btn2 are on the same position, is that right? and if that is unity will always use the event on btn1 because it is first in the code. But btn2 is showing because it is drawn afterwards.

You can only call GUI actions from within OnGUI event (drawMenu5 does not qualify…). Compiler should be warning you about this in Unity console.

(I am not 100% sure on this part) Then there’s other thing, it may matter what the transform position is for object that has the GUI script. It seemed that it has to be XYZ (0, 1, 0) so that upper left is 0, 0 pixel coordinates. If it’s 0, 0, 0 it’s possibly somewhere way off, bottom left?

if he does

void OnGUI()
{
    drawMenu5();
}

it´s ok. Because he said he can see and press the buttons. I think he does it this way.

And actually, you do nothing when the second button is pressed:

if (GUI.Button (Rect (70, 150, 120, 50), "btn1"))
{
  myBox = true;
}

GUI.Button (Rect (70, 150, 120, 50), "btn2");

can you help me about this code…i want to have a button permanent next and preview…a slideshow…
public class HorizontalTransitionGUI : MonoBehaviour
{
//A 4x4 Matrix
private Matrix4x4 trsMatrix;
//A three dimension vector that will translate GUI coordinate system
private Vector3 positionVec;
//Two booleans to determine which of the GUI buttons have been pressed
private bool next = false;
private bool back = false;

// Use this for initialization
void Start()
{
//Initialize the matrix
trsMatrix = Matrix4x4.identity;
//Initialize the Vector
positionVec = Vector3.zero;
}

// Update is called once per frame
void Update()
{
//If the ‘next’ boolean is true
if(next)
{
//Interpolate the current vector x component until it has the same as value the screen width
positionVec.x = Mathf.SmoothStep(positionVec.x, Screen.width,Time.deltaTime10);
/Make ‘trsMatrix’ a matrix that translates, rotates and scales the GUI.
The position is set to positionVec, the Quaternion is set to identity
and the scale is set to one.
/
trsMatrix.SetTRS(positionVec , Quaternion.identity, Vector3.one);
}
else if(back) //If ‘back is true’
{
//Interpolate the current vector x component until it reaches zero
positionVec.x = Mathf.SmoothStep(positionVec.x, 0,Time.deltaTime
10);
//Make ‘trsMatrix’ a matrix that translates, rotates and scales the GUI.
trsMatrix.SetTRS(positionVec , Quaternion.identity, Vector3.one);
}

}

void OnGUI()
{
//The GUI matrix must changed to the trsMatrix
GUI.matrix = trsMatrix;

//If the button labeled ‘Next’ is pressed
if(GUI.Button(new Rect(Screen.width - 400, 315, 100, 30),“Next”))
{
next = true;
back = false;
}

//The TextArea that appears on the first screen.
GUI.TextArea(new Rect(300,200,Screen.width-600,100), “Click on the ‘Next’ button to change the Text Area.”);

//If the button labeled ‘Back’ is pressed
if(GUI.Button(new Rect(-Screen.width + 300, 315, 100, 30),“Back”))
{
next = false;
back = true;
}

//The TextArea that appears on the second screen
GUI.TextArea(new Rect(-Screen.width + 300,200,Screen.width-600,100), “Click on the ‘Back’ button to return to the previous Text Area.”);

//To reset to GUI matrix, just make it equal to a 4x4 identity matrix
GUI.matrix = Matrix4x4.identity;

}
}

could you show what you want to do in a simple sketch? And do you have to use the GUI.matrix, if you can do it without, it would be much simpler.

for better read put the code in code=CSharp ] **** /code ]

i want to have a next and preview button,evrytime i click it a box or label will slide…can you help me…

  • What is the content you want to show? Pictures or/and Text?

  • Should it be 2D or 3D?

  • Do you want to use Animations for the movement or only an position change, for example move left to right in a straight line?

  • Which GUI do you want to use? Unity GUI or Unity GUI 4.6 or NGUI

pictures…it should be 2d i want to use unity gui
i have a code for a slide show…what i want is if click next and preview button the image will slide from left to right or right to left and will loop also…just like in the first code in gui matrix…it slides…
can you help me sir?

var imageArray : Texture[];
var currentImage : int;
var currentImage1 : int;
var imageRect : Rect;
var imageRect1 : Rect;
function Start()
{
    if(currentImage > 1)
    currentImage = 3;
    imageRect = Rect(100, 10,300,100);
   
        if(currentImage1 > 1)
    currentImage = 3;
    imageRect1 = Rect(100, 10,300,100);
       
}
function OnGUI()
{
    GUI.Box(imageRect, imageArray[currentImage]);
    if (GUI.Button(Rect(300,300,50,50),"next"))
        currentImage++;
       
    GUI.Box(imageRect, imageArray[currentImage]);
    if (GUI.Button(Rect(250,300,50,50),"back"))
        currentImage--;
}

i´m on it. will post it here when im ready

Currently it does not loop, but if you can´t do it yourself i will update it.

Note: this is far from being optimal, but it works :smile:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class SlideShow : MonoBehaviour
{
    public float slideSpeed = 1;
    public float spaceBetweenSlides = 100;
    public Vector2 contentSize = new Vector2(300, 250);
    public string[] contentList;    //fill in inspector
  
    int currentIdx = 0;
    Vector2 haltPos;
    Vector2 currentPos;
    Vector2 targetPos;  
    float moveDistance;
     

    void Start()
    {
        haltPos = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
        currentPos = haltPos;
        targetPos = currentPos;
    }


    void OnGUI()
    {
        // Slides
        for (int i = 0; i < contentList.Length; i++)
        {
            currentPos.x = Mathf.Lerp(currentPos.x, targetPos.x, Time.deltaTime * slideSpeed); // animate move in X
            //currentPos.y = Mathf.Lerp(currentPos.y, targetPos.y, Time.time * slideSpeed); // animate move in Y
          
            var xPos = currentPos.x - (contentSize.x * 0.5f); //center content
          
            float offset = (i * spaceBetweenSlides) + i * contentSize.x;

            //GUI.Box(imageRect, imageArray[currentImage]); <-- your content
            GUI.Label(new Rect(xPos + offset, Screen.height * 0.5f, contentSize.x, contentSize.y), contentList[i]); // <-- my test content
        }


        // Buttons
        if (currentIdx == 0)
            GUI.enabled = false;
        if (GUI.Button(new Rect(55,Screen.height *0.5f, 100, 50), "Back"))
        {
            Back();
        }
        GUI.enabled = true;


        if (currentIdx == contentList.Length-1)
            GUI.enabled = false;
        if (GUI.Button(new Rect(Screen.width - 55 - 100, Screen.height * 0.5f, 100, 50), "Next"))
        {
            Next();
        }
        GUI.enabled = true;      
    }

    void Next()
    {
        moveDistance = contentSize.x + spaceBetweenSlides;

        targetPos = new Vector2(currentPos.x - moveDistance, currentPos.y);
        currentIdx++;
    }

    void Back()
    {
        moveDistance = contentSize.x + spaceBetweenSlides;

        targetPos = new Vector2(currentPos.x + moveDistance, currentPos.y);
        currentIdx--;
    }

this is perfect sir…youre so great…gui master (Y)…i change string to textures…because there no element when i put the size of the content…thank u very much sir…thank u

1 Like

this is my exmple code…what i want is if i click the button in scene 1…this code will appear in scene 2…
if i click the button in scene1…automatically slides a box in scene2…there no key and button will press in scene2…
can you help me master… :slight_smile:

using UnityEngine;
using System.Collections;

public class region1 : MonoBehaviour {
    
    public Texture icon;
    private bool isMenuOpen = false;
    private float menuOffset =-100f;
   
    void Update() { 
       
    }
   
    void OnGUI() {
        if (GUI.Button(new Rect(300,100,30,30),"1"))  { // "Menu" button opens/closes menu.
            if (isMenuOpen) {
                StartCoroutine(SlideUp());
            } else {
                StartCoroutine(SlideDown());
            }
        }
        if (isMenuOpen) {
            // Draw your menu here, but offset it by menuOffset:
            Rect rect = new Rect(0, menuOffset, 100, 100);
            GUI.skin = style;
            GUI.Label(new Rect(50,menuOffset+70,700,450), new GUIContent (icon)); // (your GUI code here)
        }
    }
   
    IEnumerator SlideUp() {
        for (int i = 0; i < 10; i++) {
            menuOffset = -10 * i;
            yield return new WaitForSeconds(-300);
        }
        menuOffset = -50;
        isMenuOpen = false;
    }
   
    IEnumerator SlideDown() {
        isMenuOpen = true;
        for (int i = -10; i < 0; i++) {
            menuOffset = 10 * i;
            yield return new WaitForSeconds(-300);
        }
        menuOffset = -50;
    }
}

do u mean unity scenes / Levels?
Do you have Skype or some kind of messager, so we don´t spam unity forum. Send it via pm

facebook acc… pottersharry15@yahoo.com