Im trying to create a basic health system for my test project but it doesnt want to work.
I watched this in a tutorial but wont work for me.I keep getting “Cannot convert “int” tp UnityEngine.texture” error.
How could i fix this?
//Health Textures
var health76 : Texture2D;
var health75 : Texture2D;
var health74 : Texture2D;
var health73 : Texture2D;
var health72 : Texture2D;
var health71 : Texture2D;
var health70 : Texture2D;
var health69 : Texture2D;
var health68 : Texture2D;
var health67 : Texture2D;
var health66 : Texture2D;
var health65 : Texture2D;
var health64 : Texture2D;
var health63 : Texture2D;
var health62 : Texture2D;
var health61 : Texture2D;
var health60 : Texture2D;
var health59 : Texture2D;
var health58 : Texture2D;
var health57 : Texture2D;
var health56 : Texture2D;
var health55 : Texture2D;
var health54 : Texture2D;
var health53 : Texture2D;
var health52 : Texture2D;
var health51 : Texture2D;
var health50 : Texture2D;
var health49 : Texture2D;
var health48 : Texture2D;
var health47 : Texture2D;
var health46 : Texture2D;
var health45 : Texture2D;
var health44 : Texture2D;
var health43 : Texture2D;
var health42 : Texture2D;
var health41 : Texture2D;
var health40 : Texture2D;
var health39 : Texture2D;
var health38 : Texture2D;
var health37 : Texture2D;
var health36 : Texture2D;
var health35 : Texture2D;
var health34 : Texture2D;
var health33 : Texture2D;
var health32 : Texture2D;
var health31 : Texture2D;
var health30 : Texture2D;
var health29 : Texture2D;
var health28 : Texture2D;
var health27 : Texture2D;
var health26 : Texture2D;
var health25 : Texture2D;
var health24 : Texture2D;
var health23 : Texture2D;
var health22 : Texture2D;
var health21 : Texture2D;
var health20 : Texture2D;
var health19 : Texture2D;
var health18 : Texture2D;
var health17 : Texture2D;
var health16 : Texture2D;
var health15 : Texture2D;
var health14 : Texture2D;
var health13 : Texture2D;
var health12 : Texture2D;
var health11 : Texture2D;
var health10 : Texture2D;
var health9 : Texture2D;
var health8 : Texture2D;
var health7 : Texture2D;
var health6 : Texture2D;
var health5 : Texture2D;
var health4 : Texture2D;
var health3 : Texture2D;
var health2 : Texture2D;
var health1 : Texture2D;
//Intergers
static var Player_Health = 76;
//Textures
var HealthGUI : GUITexture;
switch (Player_Health)
{
case 75:
HealthGUI.texture = 75;
break;
case 74:
HealthGUI.texture = 74;
break;
case 73:
HealthGUI.texture = 73;
break;
case 72:
HealthGUI.texture = 72;
break;
case 71:
HealthGUI.texture = 71;
break;
case 70:
HealthGUI.texture = 70;
break;
case 69:
HealthGUI.texture = 69;
break;
case 68:
HealthGUI.texture = 68;
break;
case 67:
HealthGUI.texture = 67;
break;
case 66:
HealthGUI.texture = 66;
break;
case 65:
HealthGUI.texture = 65;
break;
case 64:
HealthGUI.texture = 64;
break;
case 63:
HealthGUI.texture = 63;
break;
case 62:
HealthGUI.texture = 62;
break;
case 61:
HealthGUI.texture = 61;
break;
case 60:
HealthGUI.texture = 60;
break;
case 59:
HealthGUI.texture = 59;
break;
case 58:
HealthGUI.texture = 58;
break;
case 57:
HealthGUI.texture = 57;
break;
case 56:
HealthGUI.texture = 56;
break;
case 55:
HealthGUI.texture = 55;
break;
case 54:
HealthGUI.texture = 54;
break;
case 53:
HealthGUI.texture = 53;
break;
case 52:
HealthGUI.texture = 52;
break;
case 51:
HealthGUI.texture = 51;
break;
case 50:
HealthGUI.texture = 50;
break;
case 49:
HealthGUI.texture = 49;
break;
case 48:
HealthGUI.texture = 48;
break;
case 47:
HealthGUI.texture = 47;
break;
case 46:
HealthGUI.texture = 46;
break;
case 45:
HealthGUI.texture = 45;
break;
case 44:
HealthGUI.texture = 44;
break;
case 43:
HealthGUI.texture = 43;
break;
case 42:
HealthGUI.texture = 42;
break;
case 41:
HealthGUI.texture = 40;
break;
case 40:
HealthGUI.texture = 40;
break;
case 39:
HealthGUI.texture = 39;
break;
case 38:
HealthGUI.texture = 38;
break;
case 37:
HealthGUI.texture = 37;
break;
case 36:
HealthGUI.texture = 36;
break;
case 35:
HealthGUI.texture = 35;
break;
case 34:
HealthGUI.texture = 34;
break;
case 33:
HealthGUI.texture = 33;
break;
case 32:
HealthGUI.texture = 32;
break;
case 31:
HealthGUI.texture = 31;
break;
case 30:
HealthGUI.texture = 30;
break;
case 29:
HealthGUI.texture = 29;
break;
case 28:
HealthGUI.texture = 28;
break;
case 27:
HealthGUI.texture = 27;
break;
case 26:
HealthGUI.texture = 26;
break;
case 25:
HealthGUI.texture = 25;
break;
case 24:
HealthGUI.texture = 24;
break;
case 23:
HealthGUI.texture = 23;
break;
case 22:
HealthGUI.texture = 22;
break;
case 21:
HealthGUI.texture = 21;
break;
case 20:
HealthGUI.texture = 20;
break;
case 19:
HealthGUI.texture = 19;
break;
case 18:
HealthGUI.texture = 18;
break;
case 17:
HealthGUI.texture = 17;
break;
case 16:
HealthGUI.texture = 16;
break;
case 15:
HealthGUI.texture = 15;
break;
case 14:
HealthGUI.texture = 14;
break;
case 13:
HealthGUI.texture = 13;
break;
case 12:
HealthGUI.texture = 12;
break;
case 11:
HealthGUI.texture = 11;
break;
case 10:
HealthGUI.texture = 10;
break;
case 11:
HealthGUI.texture = 11;
break;
case 10:
HealthGUI.texture = 10;
break;
case 9:
HealthGUI.texture = 9;
break;
case 8:
HealthGUI.texture = 8;
break;
case 7:
HealthGUI.texture = 7;
break;
case 6:
HealthGUI.texture = 6;
break;
case 5:
HealthGUI.texture = 5;
break;
case 4:
HealthGUI.texture = 4;
break;
case 3:
HealthGUI.texture = 3;
break;
case 2:
HealthGUI.texture = 2;
break;
case 1:
HealthGUI.texture = 1;
break;
}
function OnTriggerEnter (col : Collider)
{
if(col.gameObject.name == "cannonball(Clone)")
{
Player_Health.Player_Health -= 1;
}
}
you should consider reading up on arrays and for loops.
As for your problem, I reckon it is because you are trying to assign numbers to your healthGUI.texture.
It should instead be the ones you’ve declared in the top. Something like this should work:
HealthGUI.texture = health1;
because you’re assigning it to an integer when you do
case 21:
HealthGUI.texture = 21;
break;
case 20:
21 what? that doesn’t make much sense. I think you meant to do:
case 21:
HealthGUI.texture = health21;
break;
case 20:
Because “health21” is a 2D Texture where as 21 is just a number. So there’s your problem. And apply this to all your stuff and it runs fine. Also, I’m fairly new to programming but this seems like a terrible or atleast inconvenient way to do what you’re trying to accomplish. Maybe just have textures for every 10 or 20 health instead of every increment value. But that’s just my opinion, maybe you want it set up like this.