Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI button does not display texture

Discussion in 'Scripting' started by LucioMaximo, Dec 3, 2014.

  1. LucioMaximo

    LucioMaximo

    Joined:
    Dec 3, 2014
    Posts:
    10
    Hi,

    I am making a shop menu for a part of the game. I intended to make the shop menu out of different clickable buttons for different items. Each button is an image with a shop item and price on it. If the user wants to buy an upgrade, they click the picture, and it deducts that value from their cash.

    I have made the following image: http://i.imgur.com/VNf2fHJ.png which is to be used as the first button.

    The image is assigned via unity's inspecter onto the c# script PlayerController.cs

    public Texture shopMenuItem1;

    void OnGUI() {
    GUI.Label(new Rect (10,10,150,20), "Score: "+score);
    if (!shopMenuItem1) {
    Debug.LogError("Please assign a texture on the inspector");
    return;
    }


    if (shopMenu.Equals (true)) {
    if (GUI.Button (new Rect (50, 50, 50, 50), shopMenuItem1))
    Debug.Log ("Clicked the button with an image");
    }
    !shopMenuItem1 displays the error in the logs when I run the game, implying that no texture is assigned, even though it is assigned in the inspector: http://puu.sh/df0hT/3f0596992f.png

    I have tried assigning a gui texture instead, but it gives this error instead, which I don't understand :
    http://puu.sh/df0Bp/537ef6203d.png

    I appreciate any help you might be able to offer.

    Cheers

    Lucio
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    The screenshot shows that the texture is assigned to the script defaults, not to the gameObject in the scene. Judging by the error, it likely means that it isn't assigned properly to the actual gameObject the script is attached to.
     
  3. LucioMaximo

    LucioMaximo

    Joined:
    Dec 3, 2014
    Posts:
    10
    Ah, awesome thanks. I looked into the prefabs folder containing my player and It had a field to put another texture in. It works: http://puu.sh/df1wV/07fe1eaeae.png cheers :)
     
  4. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    No worries :)

    The field you previously assigned it to is for the script defaults, so if you've already placed the script on an object it won't appear unless you reset the script (using the little cog in the Inspector) or remove it and add it again. It would appear on any new objects you added the script to, though.