- Home /
Slider Not Working
Hi...
I am customizing my Survival Shooter and I want to insert a Slider that fills 10 points everytime an Enemy dies until 100 points. I copied the UI instructions of my HealthSlider watching the Tutorials, but it doesn't work. I tried to set my variable ShootSlider, but when I drag my ShootSlider into my script, I can't drop this inside the ShootSlider variable.
I implement my EnemyHealth code and there is my result:
using UnityEngine; using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour { public int startingHealth = 100; public int currentHealth; public Slider shootSlider; public int superPower = 10; public float sinkSpeed = 2.5f; public int scoreValue = 10; public AudioClip deathClip;
Animator anim;
AudioSource enemyAudio;
ParticleSystem hitParticles;
CapsuleCollider capsuleCollider;
bool isDead;
bool isSinking;
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
if(isSinking)
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
hitParticles.transform.position = hitPoint;
hitParticles.Play();
if (currentHealth <= 0) {
Death ();
}
shootSlider.value += superPower;
}
void Death ()
{
isDead = true;
capsuleCollider.isTrigger = true;
anim.SetTrigger ("Dead");
enemyAudio.clip = deathClip;
enemyAudio.Play ();
}
public void StartSinking ()
{
GetComponent <NavMeshAgent> ().enabled = false;
GetComponent <Rigidbody> ().isKinematic = true;
isSinking = true;
ScoreManager.score += scoreValue;
Destroy (gameObject, 2f);
}
}
Thanks guys! I'm newbie but I really want to learn.
Well... I set an Enemy into the Hierarchy and set the ShootSlider into the variable shootSlider and it works. The only mistake was put the code to add 10 points inside the method TakeDamage. I put inside the method Death and it works perfect! But I have another problem now... The Enemy isn't a Prefab, so I can clone him. How can I put this as Prefab???
One more time... Sorry about my English!!! Thanks, friends!!!