Quantcast
Channel: Questions in topic: "count"
Viewing all articles
Browse latest Browse all 187

problem count life of the player.

$
0
0
Hello! I have three scripts. 1 is (ContoVita) that counts the screw from three to zero added to a GUITEXT. All written by me is added in the inspector. using UnityEngine; using System.Collections; public class Contovita : MonoBehaviour { public static int temp = 5; void Awake () { guiText.text = ""+3; print(temp); } } ![alt text][1] The second script is (LevelManager) that allows the player to respawn when life ends. (HealthBar). In the script (LevelManager) on the line (15, 16. Then at 133, up to 138) I added the change. using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System; using UnityEditor; public class LevelManager : MonoBehaviour { public static LevelManager Instance { get; private set; } public Player Player { get; private set; } public CameraController Camera { get; private set; } public TimeSpan RunningTime { get { return DateTime.UtcNow - _started; } } public static int life = 3; public static int temp = 5; public int CurrentTimeBonus { get { var secondDifference = (int) (BonusCutoffSeconds - RunningTime.TotalSeconds); return Mathf.Max(0, secondDifference) * BonusSecondMultiplier; } } private List _checkpoints; private int _currentCheckpointIndex; private DateTime _started; private int _savedPoints; public Checkpoint DebugSpawn; public int BonusCutoffSeconds; public int BonusSecondMultiplier; public void Awake() { _savedPoints = GameManager.Instance.Points; Instance = this; } public void Start () { _checkpoints = FindObjectsOfType().OrderBy(t => t.transform.position.x).ToList(); _currentCheckpointIndex = _checkpoints.Count > 0 ? 0 : -1; Player = FindObjectOfType(); Camera = FindObjectOfType(); _started = DateTime.UtcNow; var listeners = FindObjectsOfType().OfType(); foreach (var listener in listeners) { for (var i = _checkpoints.Count - 1; i >= 0; i--) { var distance = ((MonoBehaviour)listener).transform.position.x - _checkpoints[i].transform.position.x; if (distance < 0) continue; _checkpoints[i].AssignObjectToCheckpoint(listener); break; } } #if UNITY_EDITOR if (DebugSpawn != null) DebugSpawn.SpawnPlayer(Player); else if (_currentCheckpointIndex != -1) _checkpoints[_currentCheckpointIndex].SpawnPlayer(Player); #else if (_currentCheckpointIndex != -1) _checkpoints[_currentCheckpointIndex].SpawnPlayer(Player); #endif } public void Update () { var isAtLastCheckpoint = _currentCheckpointIndex + 1 >= _checkpoints.Count; if (isAtLastCheckpoint) return; var distanceToNextCheckpoint = _checkpoints[_currentCheckpointIndex + 1].transform.position.x - Player.transform.position.x; if (distanceToNextCheckpoint >= 0) return; _checkpoints[_currentCheckpointIndex].PlayerLeftCheckpoint(); _currentCheckpointIndex++; _checkpoints[_currentCheckpointIndex].PlayerHitCheckpoint(); GameManager.Instance.AddPoints(CurrentTimeBonus); _savedPoints = GameManager.Instance.Points; _started = DateTime.UtcNow; } public void GotoNextLevel(string levelName) { StartCoroutine(GotoNextLevelCo(levelName)); } private IEnumerator GotoNextLevelCo(string levelName) { Player.FinishLevel(); GameManager.Instance.AddPoints(CurrentTimeBonus); FloatingText.Show("Level Complete!", "CheckpointText", new CenteredTextPositioner(.2f)); yield return new WaitForSeconds(1); FloatingText.Show(string.Format("{0} points!", GameManager.Instance.Points), "CheckpointText", new CenteredTextPositioner(.1f)); yield return new WaitForSeconds(5f); if (string.IsNullOrEmpty(levelName)) Application.LoadLevel("StartScreen"); else Application.LoadLevel(levelName); } public void KillPlayer() { StartCoroutine(KillPlayerCo()); } private IEnumerator KillPlayerCo() { Player.Kill(); Camera.IsFollowing = false; yield return new WaitForSeconds(2f); if(life <= 0){ HealthBar.life --; GameObject.Find("Vita3").guiText.text = ""+HealthBar.life; print("YOU NOW HAVE " + HealthBar.life + "life"); Application.LoadLevel("gameover"); } Camera.IsFollowing = true; if (_currentCheckpointIndex != -1) _checkpoints[_currentCheckpointIndex].SpawnPlayer(Player); _started = DateTime.UtcNow; GameManager.Instance.ResetPoints(_savedPoints); } } the third script (HealthBar). With the modification on the line (11,12, then 22, and 29). using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System; using UnityEditor; public class HealthBar : MonoBehaviour { public static int life = 0; public static int temp = 5; public Player Player; public Transform ForegroundSprite; public SpriteRenderer ForegroundRenderer; public Color MaxHealthColor = new Color(255 / 255f, 63 / 255f, 63 / 255f); public Color MinHealthColor = new Color(64 / 255f, 137 / 255f, 255 / 255f); void Start() { life += 3; } // Update is called once per frame public void Update () { HealthBar.life -= 1; var healthPercent = Player.Health / (float) Player.MaxHealth; ForegroundSprite.localScale = new Vector3(healthPercent, 1, 1); ForegroundRenderer.color = Color.Lerp(MaxHealthColor, MinHealthColor, healthPercent); } } Now I'm editing the two scripts (LevelManager) is (HealthBar) to load the level Gameover when (HealthBar) is performed three times. ![alt text][2] [1]: /storage/temp/50984-guitext.jpg [2]: /storage/temp/50987-x3.jpg When I start the game to test it does not work, the number (X 3) remains fixed even die itself 10 times, not counting down. Then no charge level (gameover). some advice? I hope I was clear in describing the problem. Thank You.

Viewing all articles
Browse latest Browse all 187

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>