Hello I’m developing a 2d space shooter game initially starting with quill18creates tutorial scripts but eventually going off the rails complete to make the game my own. I have animations, game menus health bars droppable hp and all kinda other cool systems I didn’t think I could make. Of course a lot of help I received from the Unity community and youtube tutorials so a big thank you to these guys that make the time to make these video tutorials and explain things in a very understandable way.
Now however I’ve encountered another problem. I’m found a tutorial on how to create a health bar and implemented in into my script, and it works I add it to the game object I assign a RectTransform in the editor and the slider moves properly. However as soon as I save the game object with a prefab with the script and assigned RectTransform. The defined RectTransform automatically goes back to none. And I cannot assign anything to it.
This is a problem because in my game the player has 3 lives. Which means a spawn script and a prefab with and assigned RectTransform to it. So since Unity allows me to assign the RectTransform only while the object exist on the scene, but not to save it in a prefab is there a way to define it directly in my scripts?
Here is my script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BetterHpScript : MonoBehaviour {
public int health = 100;
public int maxhealth = 100;
public float invulnPeriod = 0;
float invulnTimer = 0;
int correctLayer;
public RectTransform healthTransform;
public float cachedY;
public float minXvalue;
public float maxXvalue;
SpriteRenderer spriteRend;
void Start() {
cachedY = healthTransform.position.y;
maxXvalue = healthTransform.position.x;
minXvalue = healthTransform.position.x - healthTransform.rect.width;
correctLayer = gameObject.layer;
// NOTE! This only get the renderer on the parent object.
// In other words, it doesn’t work for children. I.E. “enemy01”
spriteRend = GetComponent();
if(spriteRend == null) {
spriteRend = transform.GetComponentInChildren();
if(spriteRend==null) {
Debug.LogError(“Object '”+gameObject.name+“’ has no sprite renderer.”);
}
}
}
void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.tag == “Enemy”) {
health --;
}
if (col.gameObject.tag == “Health”) {
health ++;
}
HandleHealth ();
if(invulnPeriod > 0) {
invulnTimer = invulnPeriod;
gameObject.layer = 10;
}
}
void Update() {
if (health >= maxhealth) {
health = maxhealth;
}
if(invulnTimer > 0) {
invulnTimer -= Time.deltaTime;
if(invulnTimer <= 0) {
gameObject.layer = correctLayer;
if(spriteRend != null) {
spriteRend.enabled = true;
}
}
else {
if(spriteRend != null) {
spriteRend.enabled = !spriteRend.enabled;
}
}
}
if(health <= 0) {
Die();
}
}
void Die() {
Destroy(gameObject);
}
private float MapValue (float x, float inMin, float inMax, float outMin, float outMax)
{
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
private void HandleHealth ()
{
float currentXvalue = MapValue (health, 0, maxhealth, minXvalue, maxXvalue);
healthTransform.position = new Vector3 (currentXvalue, cachedY);
}
}