- Home /
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! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613143115im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mathf.Lerp 2 Answers
How to change rect size? 2 Answers
Constant Rotation with GUI.RotateAroundPivot 1 Answer