Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by Detinator10 · Jul 01, 2014 at 03:15 AM · texture2donguiguibutton

Why won't my OnGUI() code work?

This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class ButtonScript : MonoBehaviour {
     public Texture2D icon;
     // Use this for initialization
     void Start () {
     
     }
 
     void OnGUI()
     {
         if (GUI.Button (new Rect (25, 25, 25, 25), icon)) {
                         Application.LoadLevel ("LevelSelectScreen");
                 }
     }
     
     // Update is called once per frame
     void Update () {
     }
 
 }

I have icon set as a texture but it won't show on my scene or on the game. Plus, I can't click on it. What is wrong with this code? I got it from: http://docs.unity3d.com/Manual/gui-Basics.html It might help to know that I changed my texture from a sprite to a texture in the inspector. Thank You!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Jul 01, 2014 at 03:17 AM 0
Share

Out of curiosity how can you not click on something that is not showing up?

avatar image Kiwasi · Jul 01, 2014 at 03:25 AM 0
Share

You might also want to try changing your Rect to (25,25,0,0) and adding in the curly brackets.

avatar image Detinator10 · Jul 01, 2014 at 03:34 AM 0
Share

I did that and it didn't make a difference

avatar image Detinator10 · Jul 01, 2014 at 03:38 AM 0
Share

I've updated my code

avatar image Kiwasi · Jul 01, 2014 at 04:06 AM 0
Share

Throw a debug.log into the OnGUI (outside the if) to see if the method is being called at all?

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by Detinator10 · Jul 06, 2014 at 09:31 PM

The problem was that i had the first two variables in the rect set to 25 which meant it was 25 times the camera height and width and when i changed that to .5 it worked. Here is my final code(the GUI.backgroundcolor = Color.Clear is to make the buttns invisible):

 using UnityEngine;
 using System.Collections;
 
 public class ButtonScript : MonoBehaviour {
     public Texture2D icon;
     public float posX; 
     public float posY; 
     public string LevelToLoad;
 
     // Use this for initialization
     void Start () {
 
     }
 
     void OnGUI()
     {
         GUI.backgroundColor = Color.clear;
         if (GUI.Button (new Rect (Screen.width * posX, Screen.height * posY, 200, 40), icon)) {
                         Application.LoadLevel (LevelToLoad);
                 }
     }
     
     // Update is called once per frame
     void Update () {
     }
 
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Duss · Jul 01, 2014 at 04:08 AM

I tried your code and it works fine. Make sure that your ButtonScript is in the scene you are testing. Even if you don't have any texture attached to it, you should see the default GUI button at the top left corner.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Mehul-Rughani · Jul 01, 2014 at 04:08 AM

Make Sure That You Have Assigned a Script to any Object Because In The Script There is no problem at all.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Texture2D is Null even though hooked up in Inspector. 0 Answers

how to make information slideshow? 0 Answers

Problem Displaying gameName when refreshing masterserver 0 Answers

How to make OnGUI Texture able to be clicked 1 Answer

GUITexture on/off using GUILayout.Button 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges