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
0
Question by aman_jha · May 17, 2014 at 02:54 PM · androidjavascripttexturecomponentguitexture

Change GUITexture Using UnityScript

What's up Unitarians,

I have a small GUITexture acting as a button. Quite simply, it pauses and unpauses the game upon click. The script is put on the GUITexture, which is included into the scene automatically - so its not generated via code.

The problem is that, since the script is on the actual GUITexture, I cannot change the texture of the button programmatically - as to say - whenever I click on it. I want it so that when I click on the button, it does it stuff, and it changes the image of the button.

There are questions similar to this, but in those cases, the script is not actually on the GUITexture, when in this case, it is mandatory that it is.

I'll show you the code as it is:

 #pragma strict
 
 
 public var PauseGUI : GUITexture;
 private var paused : boolean = false;
 
 public var Pause_False : Texture2D;
 public var Pause_True : Texture2D;
 
 
 /*function Start () {
 
 }*/
 
 function Update () {
     for (var touch : Touch in Input.touches) {
             if (touch.phase == TouchPhase.Began) {
                 // Construct a ray from the current touch coordinates
                 var ray = Camera.main.ScreenPointToRay (touch.position);
                 if (Physics.Raycast (ray)) { //What happens when touched
                     if (paused == false) { //Pause the game
                         paused = true;
                         Time.timeScale = 0;
                         PauseGUI.enabled = true;
                         this.texture = Pause_True; //This is where it makes no sense
                     }
                     if (paused == true) { //Unpause the game
                         paused = false;
                         Time.timeScale = 1;
                         PauseGUI.enabled = false;
                         this.texture = Pause_False; //This is where it makes no sense
                     }
                 }
             }
     }
 }


FYI, PauseGUI is NOT the button itself, it is a completely separate GUITexture that is enabled when the game is "paused"

Comment
Add comment
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

1 Reply

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

Answer by robertbu · May 17, 2014 at 03:07 PM

Solution is simple:

 guiTexture.texture = Pause_True;

GUITexture is a component. Unity typically when we want to get access to a component, we need to use GetComponnet(), but Unity provides a shortcut for some often used components like GUIText, Transform, and Rigidbody. Note if the component was not one of the ones that had shortcut, you would do:

  GetComponent(GUITexture).texture = Pause_True;

Also 'this' refers to your script/component above, not to any other component on this game object.

Comment
Add comment · Show 1 · 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 aman_jha · May 17, 2014 at 08:25 PM 0
Share

Thanks man worked like a charm!

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

20 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

Related Questions

Please Help!!! my LineRenderer Script is not show when bulid on android device 1 Answer

use HitTest to check if a screen area is touched 1 Answer

Android car controls c# 0 Answers

Disable gui texture in a different way? 0 Answers

Android: How do you position GUITexture and GUIText according to different screen sizes (JS)? 2 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