GUISkin-how to show it?

Hi all

I’ve created a new gui skin by Assets>Create>GUISkin named mainMenuGUISkin,then I set all wanted variables for button(text color to light blue and several backgrounds which are a .png files imported) and in my script I have:

public class MenuCode : MonoBehaviour
{
    GUISkin geoscapeSkin;
    
    void OnGUI()
    {
        GUI.skin=geoscapeSkin;
        GUI.Button(new Rect(Screen.width/2,Screen.height/2,300,30),"Text1","button");
        GUI.Button(new Rect(Screen.width/2,Screen.height/2+35,300,30),"Text2","button"); 
    }
}

but there is no visible effect when I hit “play”.
I suspect that GUISkin geoscapeSkin defines a default skin,not mine-but how can I change that?TIt’s not described clear enough in the documentation of customizing GUI :confused:

4 Answers

4

public class MenuCode : MonoBehaviour
{
public GUISkin geoscapeSkin; // Drag your actual gui skin asset here

    void OnGUI()
    {
        GUI.skin=geoscapeSkin;
        // Since you are using a button, you don't need to add the GUIStyle "Button"
        // It is automatically used for buttons. If you wanted to use a GUIStyle from
        // the GUISkin, other then "Button", then you would added that after the text
        if ( GUI.Button(new Rect(Screen.width/2,Screen.height/2,300,30),"Text1"))
            ButtonText1Clicked();
        if ( GUI.Button(new Rect(Screen.width/2,Screen.height/2+35,300,30),"Text2"); 
            ButtonText2Clicked();
    }
    private void ButtonText1Clicked()
    {
        // Do what ever you wanted to do here
        Debug.Log("Button Text1 was clicked!");
    }
    private void ButtonText2Clicked()
    {
        // Do what ever you wanted to do here
        Debug.Log("Button Text2 was clicked!");
    }
}

no offence but 1) you haven't posted a full code example so we can see what you might be missing (where is TakeDamage() called from?) and 2) your code example needs to be formatted correctly (the top 2 lines are just plain text) which takes like... 10 seconds to fix? putting more effort into your question and how your format it will most likely result in people being more willing to help.

–

My guess is, you’re not seeing any of the things you set in mainMenuGUISkin, because you haven’t drag’n’dropped the skin onto the geoscapeSkin variable. :slight_smile:

The problem is solved.
That drop shoud be made not on the ScriptFile->Default References component,but I had to select the object containing my script and there is a GUISkin reference the drop shoud be performed on.

Alright, great. Glad you got it solved. :P

–

You can drop it on the default references of the script but you have to re-add your script to your gameobject or rightclick it and press Reset. Then it will take the default settings that you've set up. But in most cases you want to assign references to specific instances. A skin however is a god example for a default reference since every instance needs it, so you can't forget to assign it once you've setup the default ref. ;)

–

updated (with debug logs). a common issue with animation is the state machine and how it works. For example, your issue could potentially be due to the "exit time" of an animation. This means the system will wait for that animation to finish playing before moving to the next animation state. This can cause some user input or transitions to not register. (try this). click on the animation state connection (the white line) between two animations and deselect "has exit time". Re-test with Debug logs to see if problem is resolved.

–

var skin:GUISkin;

function OnGUI(){
	GUI.skin=skin;

//other stuff here

}

I'll try that! Edit: The animations work PERFECTLY now, thanks to you. God Bless you.

–