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 navadeep2011 · Apr 01, 2013 at 10:31 AM · textureguitextureenable

How will we enable and disable a gui texture in unity 3d?

Hi Every one,

I have a small task. That is very small task but there is i don't know the error where it is. By default my texture is on position. I want that into disable.

i wrote code like this:

var someTexture : Texture2D;

 function Update () {
 
  if (Input.GetKeyDown ("space")){
     if (someTexture.enabled == true){
     someTexture.enabled = false;
     }
     else {
     someTexture.enabled = true;
     }
 }
  }

My problem here is, I attached this script to main camera and added texture in the inspector of the main camera. After then play mode. Nothing will be getting in the game scene. what can i do? i want by default that texture is in show in game scene and if i press space button that will disable .and again i press space button i want to display.

please help me. thankyou

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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by yusolaifdfer · Apr 01, 2013 at 10:45 AM

You can declare the someTexture as a GameObject, and then use

 someTexture.active = false; 

or

 someTexture.SetActive(false);

since GameObject.SetActive is obsolete now.

Comment
Add comment · Show 2 · 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 navadeep2011 · Apr 01, 2013 at 11:04 AM 0
Share

Are u telling this thing. Take one empty gui texture. in that add a texture in to inspector. Then renamed that texture in the hierarchy view. and after write code above and i attached that into main camera. then drag that texture object in the main camera inspector. but that is not working.

avatar image yusolaifdfer · Apr 01, 2013 at 11:14 AM 0
Share
 var someTexture : GUITexture;
 
 function Update () {
  
  if (Input.Get$$anonymous$$eyDown ("space")){
     if (someTexture.enabled == true){
     someTexture.enabled = false;
     }
     else {
     someTexture.enabled = true;
     }
 }
  }
avatar image
0

Answer by hexagonius · Apr 01, 2013 at 10:49 AM

The part where you actually place it is missing. You could try two different approaches:

Use the OnGUI() method to draw it into your viewport:

  • MonoBehaviour.OnGUI

  • GUI.DrawTexture

  • GUI.DrawTextureWithTexCoords

or transform the texture in your scene by using one of these two methods:

  • Camera.ViewportToWorldPoint

  • Camera.ScreenToWorldPoint

The first approach is easier to understand.

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 meat5000 ♦ · Sep 19, 2013 at 07:33 AM 0
Share

GUITexture is a part of GUILayer, so OnGUI doesn't handle it.

avatar image
0

Answer by mohammadkhatri · Apr 01, 2013 at 01:03 PM

Here Is The Problem.. In place of Texture2D U need to put GUITexture

 var someTexture : GUITexture
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 Ngo · Sep 19, 2013 at 07:26 AM

I have same problem, and I discover this way is also fine

 var showHPBar:GUITexture;
 var MaxHP:int = 1000;
 var HP:int = 500;
 
 function Update(){
   showHP();
 }
 
 function showHP(){    
 
 if(MaxHP != HP){
   showHPBar.gameObject.SetActive(true);
 }
 }
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 united4life · Sep 19, 2013 at 08:13 AM

This is a tested project written in C# and working fine.

 public GUITexture texture;
     
     void Update () {
     
         if(Input.GetKeyDown(KeyCode.Space))
         {
             if(texture.enabled == true)
                 texture.enabled = false;
             else
                 texture.enabled = true;
         }
     }
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

17 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

Related Questions

Guitexture appear/disappear 1 Answer

How can i avoid mouse clicking on alpha areas in my GUITexture? 1 Answer

How can I swap an object's texture when I click? 2 Answers

Enable a GUI texture after player collides with another object 2 Answers

Create a custom texture from code? 1 Answer


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