- Home /
how to enable/disable slider VIA script from a prefab
Ive been working on a project and I recently came up with a problem. I want when my boss to spawn, to enable a slider which will indicate its health but as the boss is a prefab I cant access the disabled slider. Is there anyway I can do this?
Here is the script for my Boss and my bossSpawner:
public class FirstBossBhv : MonoBehaviour{
public playerStats PlayerStats;
public float speed;
public Transform bosspos;
public GameObject projectile;
private bool movingRight = true;
private bool movingLeft = false;
public Rigidbody2D rb2d;
public float timeBTWshots = 2;
public float timer;
public Slider BossHealth;
float Health = 100;
bool isAlive = true;
private void Start()
{
PlayerStats = GameObject.Find("player").GetComponent<playerStats>();
BossHealth = GameObject.Find("Canvas").GetComponent<Slider>();
}
void Update()
{
if (movingRight == true)
{
rb2d.velocity = transform.right * speed;
} else if (movingLeft == true)
{
rb2d.velocity = transform.right * -speed;
}
timer += Time.deltaTime;
if(Mathf.Round(timer) == timeBTWshots)
{
Instantiate(projectile,bosspos.position,Quaternion.identity);
timer = 0;
}
if(Health == 0)
{
isAlive = false;
Destroy(this.gameObject);
}
if(isAlive == true)
{
BossHealth.gameObject.SetActive(true);
}else if(isAlive == false)
{
BossHealth.gameObject.SetActive(false);
}
BossHealth.value = Health;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("2Walls"))
{
if (movingRight == true && movingLeft == false)
{
movingRight = false;
movingLeft = true;
} else if (movingRight == false && movingLeft == true)
{
movingRight = true;
movingLeft = false;
}
}
if (collision.gameObject.CompareTag("pLAYER"))
{
Health -= PlayerStats.dmg;
}
}
}
public class BossSpawn : MonoBehaviour
{
public GameObject boss;
playerStats PlayerStats;
enemySpawner EnemySpawner;
void Start()
{
PlayerStats = GameObject.Find("player").GetComponent<playerStats>();
EnemySpawner = GameObject.Find("Spawner").GetComponent<enemySpawner>();
}
void Update()
{
if(PlayerStats.score == 2 && EnemySpawner.Waves == true)
{
PlayerStats.score = 0;
EnemySpawner.Waves = false;
StartCoroutine(bossApperance());
}
}
IEnumerator bossApperance()
{
yield return new WaitForSeconds(5);
Instantiate(boss,this.transform.position,Quaternion.identity);
}
Comment
Your answer
Follow this Question
Related Questions
Enable/Disable a Static object 1 Answer
Enable/Disable children / group of objects 0 Answers
enable/disable child objects? 2 Answers
enable/disable private gameObject 1 Answer