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 Forskan · Apr 19, 2014 at 07:21 PM · texture2donguidrawtexture

Changing Texture2D at runtime, but it doesent work?

Hey, im just drawing some textures(ability buttons) to the screen, and if an ability is on cooldown then I just want to change the texture of the "button" to my cooldown texture but I cant get it to work.. here is my code:

 private Target _targetScript;

 private Champion_A__FirstAbility _firstAbility;

 private Champion_A__SecondAbility _secondAbility;

 private Champion_A__ThirdAbility _thirdAbility;

 private Champion_A__UltimateAbility _ultimateAbility;


 public float _hpBarLength = 0;

 public Texture2D _hpBarTexture;
 public Texture2D _manaBarTexture;
 public Texture2D _backgroundBarTexture;

 public Texture2D _firstAbilityTexture;
 public Texture2D _secondAbilityTexture;
 public Texture2D _thirdAbilityTexture;
 public Texture2D _ultimateAbilityTexture;

 private Texture2D _firstTexture; 
 private    Texture2D _secondTexture;
 private Texture2D _thirdTexture;
 private Texture2D _ultimateTexture;    

 public Texture2D _abilityCoolDownTexture;

 private float _abilityButtonWidth;
 private float _abilityButtonHeight;

 // Use this for initialization
 void Start () {
     _abilityButtonWidth = _firstAbilityTexture.width * 0.5f;  // They are 2 big, so I felt like making them half the size. hence " * 0.5f "
     _abilityButtonHeight = _firstAbilityTexture.height * 0.5f;

     //Getting the scripts.
     _targetScript = (Target)GetComponent<Target> ();
     _firstAbility = (Champion_A__FirstAbility)GetComponent(typeof(Champion_A__FirstAbility));
     _secondAbility = (Champion_A__SecondAbility)GetComponent<Champion_A__SecondAbility> ();
     _thirdAbility = (Champion_A__ThirdAbility)GetComponent<Champion_A__ThirdAbility> ();
     _ultimateAbility = (Champion_A__UltimateAbility)GetComponent<Champion_A__UltimateAbility> ();

 }
 
 // Update is called once per frame
 void Update () {
     float percentOfHP = _targetScript.Health / _targetScript.MaxHealth;
     _hpBarLength = percentOfHP * 100;
 
     
     CoolDownCheck();
 }

 void OnGUI()
 {
     CoolDownCheck();

     if (_targetScript.Health > 0)
     {
         GUI.DrawTexture(new Rect(100, 10, 0, 10), _backgroundBarTexture);
         GUI.DrawTexture(new Rect(100, 10, _hpBarLength, 10), _hpBarTexture);
     }


     // First Ability
     GUI.DrawTexture(new Rect((Screen.width/2) - 160,
                              Screen.height - 100,
                              _abilityButtonWidth,
                              _abilityButtonHeight),
                     _firstTexture);

     // Second Ability
     GUI.DrawTexture(new Rect((Screen.width/2 - 80),
                              Screen.height - 100,
                              _abilityButtonWidth,
                              _abilityButtonHeight),
                     _secondTexture);

     // Third Ability
     GUI.DrawTexture(new Rect((Screen.width/2),
                              Screen.height - 100,
                              _abilityButtonWidth,
                              _abilityButtonHeight),
                     _thirdTexture);
 
     // ULTIMATE
     GUI.DrawTexture(new Rect((Screen.width/2 + 80),
                              Screen.height - 100,
                              _abilityButtonWidth,
                              _abilityButtonHeight),
                     _ultimateTexture);

 }


 private void CoolDownCheck()
 {
     _firstTexture = _firstAbilityTexture;
     if(_firstAbility.AbilityReady())
     {
         _firstTexture = _abilityCoolDownTexture;
     }
  

  
     _secondTexture = _secondAbilityTexture;
     if(_secondAbility.AbilityReady())
     {
         _secondTexture = _abilityCoolDownTexture;
     }
  

  
     _thirdTexture = _thirdAbilityTexture;
     if(!_thirdAbility.AbilityReady())
     {
         _thirdTexture = _abilityCoolDownTexture;
         Debug.Log("VI KOMMER IN HÄR, VI SKA HA COOLDOWN TEXTURE!!");
     }
  

  
     _ultimateTexture = _ultimateAbilityTexture;
     if(!_ultimateAbility.AbilityReady())
     {
         _ultimateTexture = _abilityCoolDownTexture;
     }
      
 }




the "Target" script is actually where I get my own hp, incase someone would ask about that.

At first I had methods for returning the texture, that I used in the GUI.DrawTexture()... so instead of

GUI.DrawTexture(new Rect((Screen.width/2) - 160, Screen.height - 100, _abilityButtonWidth, _abilityButtonHeight), _firstTexture); <--- this

I had

GUI.DrawTexture(new Rect((Screen.width/2) - 160, Screen.height - 100, _abilityButtonWidth, _abilityButtonHeight), FirstAbilityTexture()); <--- this

private Texture2D FirstAbilityTexture(){

  if(isOnCoolDown)
  {
       return _abilityCoolDownTexture;
  }

  return _firstAbilityTexture;

}

but that didnt work either.

Please help a newbie programmer! :)

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

0 Replies

· Add your reply
  • Sort: 

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

Mathf.Lerp 2 Answers

How to change rect size? 2 Answers

Constant Rotation with GUI.RotateAroundPivot 1 Answer

How to drawing 2 image in one Texture2d 1 Answer

Create Texture2D elements in OnGUI Method 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