Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

LifeGauge

0
Last updated at Posted at 2021-01-10

ゲーム「ミニトマト収穫祭」のPlayerオブジェクトのスクリプト
・ライフの増減
・Gameover/Gameclearの処理

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LifeGauge : MonoBehaviour
{
    [SerializeField]
    private GameObject lifeObj = null;
    public GameObject gameoverText;//GameoverTextを取得
    public GameObject gameClearText;//GameClearTextを取得
    public GameObject Btn;
    public GameObject Btn2;
    int gameover = 3;
    PlayerController playerController;
    KabuGenerate kabuGenerate;
    public static int setlife ;


    private void Start()
    {
        gameoverText.SetActive(false);
        gameClearText.SetActive(false);
        Btn.SetActive(false);       
        SetLifeGauge(3);
        playerController = FindObjectOfType<PlayerController>();
        kabuGenerate = FindObjectOfType<KabuGenerate>();
    }



    //ライフゲージ全削除&HP分作成
    public void SetLifeGauge(int life)
    {
        for (int i = 0; i < transform.childCount; i++)//体力を一旦全削除
        {
            Destroy(transform.GetChild(i).gameObject);
        }       
        for (int i = 0; i < life; i++)//現在の体力数分のライフゲージを作成
        {
            Instantiate<GameObject>(lifeObj, transform);
        }
    }

    //回復
    public void SetLifeGauge3(int num)
    {
        Instantiate<GameObject>(lifeObj, transform);
        gameover += 1;
    }

    // ダメージ分だけ削除
    public void SetLifeGauge2(int damage)
    {
        for (int i = 0; i < damage; i++)
        {
            Destroy(transform.GetChild(i).gameObject);//最後のライフゲージを削除         
            gameover -= 1;
            if (gameover == 0)
            {
                pause(0f,0f);
                GameOver();
            }
        }
    }

    //ゲームオーバー関数
    public void GameOver()
    {
        gameoverText.SetActive(true);
        Btn.SetActive(true);
        Btn2.SetActive(true);
    }

    //ゲームクリア関数
    public void GameClear()
    {
        gameClearText.SetActive(true);
        pause(0f, 0f);
        Invoke("Clear", 2);
    }

    //一時停止関数
    public void pause(float x, float y)
    {
        playerController.gameSpeed = x;
        kabuGenerate.timeleft = y;
    }
    
    //2秒後にオブジェクトを消す
    public void Clear()
    {
        SceneManager.LoadScene("Clear");
    }
}
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?