Howdy all,
First post. I’m working on creating a Health Bar for a Platformer. I’ve gotten it setup so that it creates a quad, allows for scaling/positioning of the quad, in so far as I can tell…it loads the textures, and then if I understand the API (This is also the first time I’ve ever scripted in Unity) should be acessing and assignining materials and textures…but I just get the standard grey material on my Quad instead of my texture. Here’s the code, any and all help is greatly appreciated!
V/R
Brad
using UnityEngine;
using System.Collections;
public class PlayerHealthBar : MonoBehaviour
{
//Create the HealthBar Quad.
GameObject HealthBar;
//Create 2 Vector3's to modify Position and Scale.
public Vector3 HealthBarPosition;
public Vector3 HealthBarScale;
//Create an Array to hold all of the textures for the Health Bar.
Texture [] HealthBarTexture = new Texture[17];
//Create our Material.
Material HealthBarMaterial = null;
// Use this for initialization
void Start ()
{
HealthBar = GameObject.CreatePrimitive(PrimitiveType.Quad);
//Transform the local position of hte Bar.
HealthBar.transform.localPosition = HealthBarPosition;
//Transform the Scale of the Health Bar.
HealthBar.transform.localScale = HealthBarScale;
//Load the HealthBar Textures.
HealthBarTexture [0] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_016.png") as Texture;
HealthBarTexture [1] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_015.png") as Texture;
HealthBarTexture [2] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_014.png") as Texture;
HealthBarTexture [3] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_013.png") as Texture;
HealthBarTexture [4] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_012.png") as Texture;
HealthBarTexture [5] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_011.png") as Texture;
HealthBarTexture [6] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_010.png") as Texture;
HealthBarTexture [7] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_009.png") as Texture;
HealthBarTexture [8] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_008.png") as Texture;
HealthBarTexture [9] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_007.png") as Texture;
HealthBarTexture [10] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_006.png") as Texture;
HealthBarTexture [11] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_005.png") as Texture;
HealthBarTexture [12] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_004.png") as Texture;
HealthBarTexture [13] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_003.png") as Texture;
HealthBarTexture [14] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_002.png") as Texture;
HealthBarTexture [15] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_001.png") as Texture;
HealthBarTexture [16] = Resources.Load ("Assets/UI/Player Health Bar/Textures/playerHealthBar_000.png") as Texture;
//This should apply the material to the HealthBar...
HealthBar.gameObject.GetComponent<Renderer> ().material = HealthBarMaterial;
//This should apply the texture to the HealthBar...
HealthBar.gameObject.GetComponent<Renderer>().material.mainTexture = HealthBarTexture [4];
}
// Update is called once per frame
void Update ()
{
}
}
Handful of little of things:
-
remove the .png suffix. Cannot use that in Unity… just the base name of the resource.
-
second of all, everything that is loaded via Resources.Load() has got to be under a folder (anywhere) called Resources.
-
finally, just to make sure you get what you want, you should always use the templated version of Resources.Load(), in this case Resources.Load( …);
That should get you going!
Updated my code based on your direction and I’m still not getting the texture the load. I’ve now got the folders setup as follows:
Assets/Resources/playerHealthBar
I also made sure all the images are Textures…The object is now bright pink, so something is happening now.
The "Assets/…was a test, so far none of the textures are loading at all…or at least not displaying.
Here’s the updated code:
using UnityEngine;
using System.Collections;
public class PlayerHealthBar : MonoBehaviour
{
//Create the HealthBar Quad.
GameObject HealthBar;
//Create 2 Vector3's to modify Position and Scale.
public Vector3 HealthBarPosition;
public Vector3 HealthBarScale;
//Create an Array to hold all of the textures for the Health Bar.
Texture [] HealthBarTexture = new Texture[17];
//Create our Material.
Material HealthBarMaterial = null;
// Use this for initialization
void Start ()
{
HealthBar = GameObject.CreatePrimitive(PrimitiveType.Quad);
//Transform the local position of hte Bar.
HealthBar.transform.localPosition = HealthBarPosition;
//Transform the Scale of the Health Bar.
HealthBar.transform.localScale = HealthBarScale;
//Load the HealthBar Textures.
HealthBarTexture [0] = Resources.Load<Texture> ("Assets/Resources/playerHealthBar/playerHealthBar_016");
HealthBarTexture [1] = Resources.Load<Texture> ("playerHealthBar_015");
HealthBarTexture [2] = Resources.Load<Texture> ("playerHealthBar_014");
HealthBarTexture [3] = Resources.Load<Texture> ("playerHealthBar_013");
HealthBarTexture [4] = Resources.Load<Texture> ("playerHealthBar_012");
HealthBarTexture [5] = Resources.Load<Texture> ("playerHealthBar_011");
HealthBarTexture [6] = Resources.Load<Texture> ("playerHealthBar_010");
HealthBarTexture [7] = Resources.Load<Texture> ("playerHealthBar_009");
HealthBarTexture [8] = Resources.Load<Texture> ("playerHealthBar_008");
HealthBarTexture [9] = Resources.Load<Texture> ("playerHealthBar_007");
HealthBarTexture [10] = Resources.Load<Texture> ("playerHealthBar_006");
HealthBarTexture [11] = Resources.Load<Texture> ("playerHealthBar_005");
HealthBarTexture [12] = Resources.Load<Texture> ("playerHealthBar_004");
HealthBarTexture [13] = Resources.Load<Texture> ("playerHealthBar_003");
HealthBarTexture [14] = Resources.Load<Texture> ("playerHealthBar_002");
HealthBarTexture [15] = Resources.Load<Texture> ("playerHealthBar_001");
HealthBarTexture [16] = Resources.Load<Texture> ("playerHealthBar_000");
//This should apply the material to the HealthBar...
HealthBar.gameObject.GetComponent<Renderer> ().material = HealthBarMaterial;
//This should apply the texture to the HealthBar...
HealthBarMaterial.mainTexture = HealthBarTexture [0];
}
// Update is called once per frame
void Update ()
{
}
}
Excellent work. The bright pink means your texture is null, which means you are not loading those images.
Those files are probably not where you are telling Unity to get them. If they are to be loaded, like I said they need to be under some folder (anywhere) named Resources. They may be prefixed with a partial path however, so if you just dragged your UI folder into Resources, then you would still need to dig into UI/Player Health Bar/Textures/ explicitly for each file.
Awesome! Thanks for the help, it’s working like a champ now. The big thing I had to do was actually create an attachable Material to do work on. Or at least that’s how I got it to work. Works really well too!
V/R
Brad
using UnityEngine;
using System.Collections;
public class PlayerHealthBar : MonoBehaviour
{
public int PlayerMaximumHealth = 64;
public int PlayerCurrentHealth = 64;
int HealthBarIndex = 16;
//Create the HealthBar Quad.
GameObject HealthBar;
//Create 2 Vector3's to modify Position and Scale.
public Vector3 HealthBarPosition;
public Vector3 HealthBarScale;
//Create an Array to hold all of the textures for the Health Bar.
Texture2D [] HealthBarTexture = new Texture2D[17];
//Create our Material.
public Material HealthBarMaterial;
// Use this for initialization
void Start ()
{
HealthBar = GameObject.CreatePrimitive(PrimitiveType.Quad);
//Transform the local position of hte Bar.
HealthBar.transform.localPosition = HealthBarPosition;
//Transform the Scale of the Health Bar.
HealthBar.transform.localScale = HealthBarScale;
//Load the HealthBar Textures.
HealthBarTexture [0] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_016");
HealthBarTexture [1] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_015");
HealthBarTexture [2] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_014");
HealthBarTexture [3] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_013");
HealthBarTexture [4] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_012");
HealthBarTexture [5] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_011");
HealthBarTexture [6] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_010");
HealthBarTexture [7] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_009");
HealthBarTexture [8] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_008");
HealthBarTexture [9] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_007");
HealthBarTexture [10] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_006");
HealthBarTexture [11] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_005");
HealthBarTexture [12] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_004");
HealthBarTexture [13] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_003");
HealthBarTexture [14] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_002");
HealthBarTexture [15] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_001");
HealthBarTexture [16] = Resources.Load<Texture2D> ("PlayerHealthBar/playerHealthBar_000");
//This should apply the material to the HealthBar...
HealthBar.gameObject.GetComponent<Renderer> ().material = HealthBarMaterial;
}
// Update is called once per frame
void Update ()
{
HealthBarIndex = PlayerCurrentHealth / 4;
//This updates the Player Health Bar
if (HealthBarIndex == 0)
{
HealthBarMaterial.mainTexture = HealthBarTexture [16];
}
else if (HealthBarIndex == 1)
{
HealthBarMaterial.mainTexture = HealthBarTexture [15];
}
else if (HealthBarIndex == 2)
{
HealthBarMaterial.mainTexture = HealthBarTexture [14];
}
else if (HealthBarIndex == 3)
{
HealthBarMaterial.mainTexture = HealthBarTexture [13];
}
else if (HealthBarIndex == 4)
{
HealthBarMaterial.mainTexture = HealthBarTexture [12];
}
else if (HealthBarIndex == 5)
{
HealthBarMaterial.mainTexture = HealthBarTexture [11];
}
else if (HealthBarIndex == 6)
{
HealthBarMaterial.mainTexture = HealthBarTexture [10];
}
else if (HealthBarIndex == 7)
{
HealthBarMaterial.mainTexture = HealthBarTexture [9];
}
else if (HealthBarIndex == 8)
{
HealthBarMaterial.mainTexture = HealthBarTexture [8];
}
else if (HealthBarIndex == 9)
{
HealthBarMaterial.mainTexture = HealthBarTexture [7];
}
else if (HealthBarIndex == 10)
{
HealthBarMaterial.mainTexture = HealthBarTexture [6];
}
else if (HealthBarIndex == 11)
{
HealthBarMaterial.mainTexture = HealthBarTexture [5];
}
else if (HealthBarIndex == 12)
{
HealthBarMaterial.mainTexture = HealthBarTexture [4];
}
else if (HealthBarIndex == 13)
{
HealthBarMaterial.mainTexture = HealthBarTexture [3];
}
else if (HealthBarIndex == 14)
{
HealthBarMaterial.mainTexture = HealthBarTexture [2];
}
else if (HealthBarIndex == 15)
{
HealthBarMaterial.mainTexture = HealthBarTexture [1];
}
else if (HealthBarIndex == 16)
{
HealthBarMaterial.mainTexture = HealthBarTexture [0];
}
}
}
I am happy for you! But I also urge you to now go and learn about for() loops, and how you can compress the above script into FAR fewer lines of code while drastically improving reliability and debuggability. It will make you smile, and you’ll save a lot of keyboards through the coming years.
2 Likes
Hahah, you’re right. I went brute force to test and then didn’t condense. I can definitely shrink the image changer a lot with a good for loop! Thanks again for the assist!