GUI.DrawTexture disappering after a new one is made.

Hi, i have a problem with GUI.DrawTexture that i need some help with.

I want to draw a GUI Texture when my player enters one trigger, and make an other GUI Texture 50 pixels lower when it enters an other trigger.

What my code is supposed to do is the following:

  • If the player triggers “trigger1” a GUI is made.
  • If the player triggers “choice1” a GUI is made 50 pixels lower than the previous.
  • If the player triggers “choice2” a GUI is made 50 pixels lower than the previous.
  • If the player triggers “choice3” a GUI is made 50 pixels lower than the precious.

The problem is:

When a GUI is made from the triggers “choice1”, “choice2” or “choice3”, any other GUI generated from the ‘choice’ triggers disappears.

Example:

  • Player enters “trigger1” → GUI is made
  • Player enters “choice2” → GUI is made
  • Player enters “choice3” → GUI is made but GUI from “choice2” is removed
    :frowning:

The GUI textures is made from collisions turning booleans ‘true’ and if-statements actually creating the GUI texture when a certain boolean is true.

My question:

Why is the GUI textures from the ‘choices’ disappearing? Even when the boolean values stays true?

My code:

public int choicesMade = 0; //Variable for lowering GUI and controlling their textures
public Texture2D[] noteTextures; //Array with textures
public bool noteOpen = false; //Boolean for creating GUI
public bool choice1 = false; //Boolean for creating GUI
public bool choice2 = false; //Boolean for creating GUI
public bool choice3 = false; //Boolean for creating GUI

//Controls for positioning and scaling of GUI texture
public float notePosX;
public float notePosY;
public float noteHeight;
public float noteWidth;
public int noteOffset; //Variable for how much GUI should be lowered

void OnTriggerEnter(Collider collision) //Collisions
{
    if (collision.tag == "trigger1") //If collides with 'trigger1'
    {
        noteOpen = true; //Makes boolean true
    }

    if (collision.tag == "choice1") //If collides with 'choice1'
    {
        choice1 = true; //Makes boolean true
    }

    if (collision.tag == "choice2") //If collides with 'choice2'
    {
        choice2 = true; //Makes boolean true
    }

    if (collision.tag == "choice3") //If collides with 'choice3'
    {
        choice3 = true; //Makes boolean true
    }

    if (collision.tag == "choice1" || collision.tag == "choice2" || collision.tag == "choice3") //If any of the choices is hit
    {
        choicesMade++; //+1 to choicesMade
    }
}

void OnGUI() //Controls for making GUI
{
    if (noteOpen) //If boolean is true
    {
            GUI.DrawTexture(new Rect(Screen.width / 100 * notePosX, Screen.height / 100 * notePosY, noteWidth, noteHeight), noteTextures[0], ScaleMode.ScaleToFit, true, 10.0f);
    }

    if (choice1) //If boolean is true
    {
        GUI.DrawTexture(new Rect(Screen.width / 100 * notePosX, (Screen.height / 100 * notePosY) + (noteOffset * choicesMade), noteWidth, noteHeight), noteTextures[choicesMade], ScaleMode.ScaleToFit, true, 10.0f);
    }

    if (choice2) //If boolean is true
    {
        GUI.DrawTexture(new Rect(Screen.width / 100 * notePosX, (Screen.height / 100 * notePosY) + (noteOffset * choicesMade), noteWidth, noteHeight), noteTextures[choicesMade], ScaleMode.ScaleToFit, true, 10.0f);
    }

    if (choice3) //If boolean is true
    {
        GUI.DrawTexture(new Rect(Screen.width / 100 * notePosX, (Screen.height / 100 * notePosY) + (noteOffset * choicesMade), noteWidth, noteHeight), noteTextures[choicesMade], ScaleMode.ScaleToFit, true, 10.0f);
    }
}

You are drawing all choices in the same place (choicesMade * noteOffset). A simpler and better way to do that without this problem is to calculate the first rectangle at the beggining of OnGUI(), and add noteOffset to its y coordinate before drawing each choice (you can remove choicesMade):

void OnGUI() //Controls for making GUI
{
    Rect rect = new Rect(Screen.width / 100 * notePosX, Screen.height / 100 * notePosY, noteWidth, noteHeight);
    if (noteOpen) //If boolean is true
    {
            GUI.DrawTexture(new Rect(, noteTextures[0], ScaleMode.ScaleToFit, true, 10.0f);
    }

    if (choice1) //If boolean is true
    {
        rect.y += noteOffset; // shifts rect by noteOffset
        GUI.DrawTexture(rect, noteTextures[choicesMade], ScaleMode.ScaleToFit, true, 10.0f);
    }

    if (choice2) //If boolean is true
    {
        rect.y += noteOffset; // shifts rect by noteOffset
        GUI.DrawTexture(rect, noteWidth, noteHeight), noteTextures[choicesMade], ScaleMode.ScaleToFit, true, 10.0f);
    }

    if (choice3) //If boolean is true
    {
        rect.y += noteOffset; // shifts rect by noteOffset
        GUI.DrawTexture(rect, noteTextures[choicesMade], ScaleMode.ScaleToFit, true, 10.0f);
    }
}