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 Meldowns · May 12, 2015 at 09:54 PM · javascriptguitextureimageraw

How do I change the texture of a Raw image?

I have created a UI RawImage of a battery, and throughout the game as the player collects batteries the life on the battery grows. This requires me the change the texture of my Raw Image. How do I script that using Javascript in Unity 4.6.

Here is what I have

 #pragma strict
 
 
 static var charge : int = 0;
 var charge1tex: Texture2D;
 var charge2tex : Texture2D;
 var charge3tex : Texture2D;
 var charge4tex : Texture2D;
 var charge0tex : Texture2D;
 
 function Start () {
     
     guiTexture.enabled = false; //exécutre au début du niveau du jeu la pile n'est pas visible au début du jeu
     charge = 0; //Aucun pile été collectionner des le début du jeu
 }
 
 function Update () {
     if (charge ==1){
         UI.RawImage.texture = charge1tex;  
         UI.RawImage.enabled = true; //I only want it visible once I gain some battery power.
     }
     
     else if (charge ==2){
         guiTexture.texture = charge2tex;
     }
     else if (charge ==3){
         guiTexture.texture = charge3tex;
     }
     else if (charge ==4){
         guiTexture.texture = charge4tex;
     }
     else{
         guiTexture.texture = charge0tex;
     }
 }


Thanks for your help

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
0

Answer by DwaynePritchett · May 13, 2015 at 10:01 PM

Don't use texture 2d. Use, Sprites. Make an image by right clicking the hierarchy view and selecting create-image. Then, add a public variable to your script, public Image battery. Add using Unity.UI; at the top of course. Now, change the Image.fillAmount, to equal charge/fullcharge. Inside the inspector, set the image to fill horizontally/vertically. Now, you have a nice "Gauge" like hp, but for the battery. If you want it to look like a battery, and fill the inside. Make 2 images. The parent should be the battery shape... add a mask component to it. The child will be the first image I explained to you. If this doesn't make sense. Go check out a Unity UI 4.6 tutorial. Sorry, if this isn't that helpful.

-Dwayne Pritchett

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 Meldowns · May 14, 2015 at 06:02 PM 0
Share

O$$anonymous$$ so I changed my images to Sprites and am able to put the Image source on my UI Image to the first one I want. Now how do I code the change of the Image source of my UI Image when it hits a battery. $$anonymous$$y UI image is called battery_life.

 static var charge : int = 0;
 var charge1 : Sprite;
 var charge2 : Sprite;
 var charge3 : Sprite;
 var charge4 : Sprite;
 var charge0 : Sprite;
 
 
 function Update () {
     if (charge ==1){
       //What goes here to make the Image source to be the image stored in charge1?
     }


avatar image DwaynePritchett · May 14, 2015 at 10:04 PM 0
Share
  static var charge : int = 0;
  var charge1 : Sprite;
  var charge2 : Sprite;
  var charge3 : Sprite;
  var charge4 : Sprite;
  var charge0 : Sprite;
  public var batteryImage : Image;
  
  
  function Update () {
      if (charge ==1){
        batteryImage.sprite = charge1;
      }

You have to drag and drop the Image onto your public image variable in the inspector though. Or you can find it in code... if you want to "find it"... look up referencing objects.

I don't use UnityScript so there might be an error. Also, this isn't really a good way to do this... but it should work none the less. Hope this helps, -Dwayne Pritchett

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I add a distance cap to this script? 3 Answers

How can i show the texture in the array? 2 Answers

DrawTexture GUI iPhone 1 Answer

GUI Texture fixed on Object 1 Answer

How do I trigger GUI elements 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