This question was
closed Mar 09, 2020 at 11:17 AM by
tormentoarmagedoom for the following reason:
Too subjective and argumentative
Question by
dipeshjethwa63 · Mar 09, 2020 at 07:22 AM ·
updatescorebarhealth
how to update score and health throughout different scenes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public static PlayerHealth Instance;
public int health;
public GameObject deathEffect;
void Awake()
{
if (Instance == null)
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
else if (Instance != this)
{
Destroy(gameObject);
}
FindObjectOfType(typeof(Transform));
}
public void TakeDamage(int damage)
{
health -= damage;
StartCoroutine(DamageAnimation());
if (health <= 0)
{
Die();
}
}
void Die()
{
SceneManager.LoadScene("Game Over");
}
public void Damage(int dmg)
{
health -= dmg;
}
IEnumerator DamageAnimation()
{
SpriteRenderer[] srs = GetComponentsInChildren<SpriteRenderer>();
for (int i = 0; i < 3; i++)
{
foreach (SpriteRenderer sr in srs)
{
Color c = sr.color;
c.a = 0;
sr.color = c;
}
yield return new WaitForSeconds(.1f);
foreach (SpriteRenderer sr in srs)
{
Color c = sr.color;
c.a = 1;
sr.color = c;
}
yield return new WaitForSeconds(.1f);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemyflip : MonoBehaviour
{
public Transform player;
public bool isFlipped = false;
public void LookAtPlayer()
{
Vector3 flipped = transform.localScale;
flipped.z *= -1f;
if (transform.position.x > player.position.x && isFlipped)
{
transform.localScale = flipped;
transform.Rotate(0f, 180f, 0f);
isFlipped = false;
}
else if (transform.position.x < player.position.x && !isFlipped)
{
transform.localScale = flipped;
transform.Rotate(0f, 180f, 0f);
isFlipped = true;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public PlayerHealth playerHealth;
public Slider slider;
void Start()
{
slider.maxValue = playerHealth.health;
DontDestroyOnLoad(gameObject);
}
// Update is called once per frame
void Update()
{
slider.value = playerHealth.health;
}
internal void SetmaxHealth(int maxHealth)
{
throw new NotImplementedException();
}
internal void SetHealth(int maxHealth)
{
throw new NotImplementedException();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour
{
//static ScoreController Instance;
public Text scoreText ;
private int score;
void Start()
{
score = 0;
UpdateScore();
}
public void AddScore(int newScorevalue)
{
score += newScorevalue;
UpdateScore();
}
// Update is called once per frame
void UpdateScore()
{
scoreText.text= "Score:" + score;
}
}
public class ScoreController : MonoBehaviour
{
public Text scoreText ;
private int score;
void Start()
{
score = 0;
UpdateScore();
}
public void AddScore(int newScorevalue)
{
score += newScorevalue;
UpdateScore();
}
// Update is called once per frame
void UpdateScore()
{
scoreText.text= "Score:" + score;
}
}
Comment
Best Answer
Answer by tormentoarmagedoom · Mar 09, 2020 at 11:17 AM
Hello.
First, this is not a script service provider..
Do not post your code and wait for people to give youthe code.
Second, go learn about data persistance between scenes.
Post closed.
Follow this Question
Related Questions
How can i Reset Health,Score,Time??? 0 Answers
Score, FixedUpdate and Update 0 Answers
Score wont display 0 Answers
My health bars image.fillAmount doesnt change. 4 Answers
Opening Project Error 0 Answers