유니티 기초
게임오버씬
노재두내
2023. 8. 7. 18:00
잘 안됐던것 : Find해서 GetConponent 하는 부분이 안됐음 연습 해야할듯 , instantiate가 어러개 만들때 쓰는건줄 알았는데 하나만 만드는것도 가능했음 --> 제대로 찾아보고 비교해보기
int rand=Random.Range(0, 11);// 1~10사과가 나오는 횟수를 더 늘리고싶음
GameObject itemGo = null;
if (rand>3)
{
itemGo= Instantiate<GameObject>(this.applePrefab);
}
if (this.elapsedTime > 1f) {
//화살을 만든다
this.CreateArrow();
//경과시간 초기화
this.elapsedTime = 0; //0초부터 1초까지를 셀수 있도록
}
private void CreateArrow()
{
//arrowGo : 프리팹 복제본 (인스턴스)
GameObject arrowGo = Instantiate(this.arrowPrefab);
//Random.Range(-7.5f, -7.5f)
arrowGo.transform.position = new Vector3(Random.Range(-7, 8), 6f, 0);
}
이런식으로 조건이 참인동안인 경우에는 여러번 생성되고
GameObject basketGo = Instantiate(prefab);
이건 한번 생성
unity document를 보니
-----> 주석에 나와있듯이 게임이 시작할때 simply instantiate 즉, 한번만 이라는 뜻같음
----> for문 도는동안 복제
좀 이해된당
using Palmmedia.ReportGenerator.Core.Reporting.Builders;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
[SerializeField]
private Text txtTime;
[SerializeField]
private Text txtScore;
private float time = 5.0f;
private int score = 0;
private bool isGameOver = false;
// Start is called before the first frame update
void Start()
{
this.UpdateScoreUI();
}
// Update is called once per frame
void Update()
{
if (this.isGameOver) return;
this.time -= Time.deltaTime;
this.txtTime.text = this.time.ToString("F1");//소수점 1자리까지 표시
if (this.time <= 0)
{
if (this.isGameOver == false)
{
Debug.Log("game over");
this.isGameOver = true;
this.LoadGameOverScene();
}
}
}
public void UpdateScoreUI()
{
this.txtScore.text = string.Format("{0} Point", score);
}
public void IncreaseScore(int score)
{
this.score += score;
this.UpdateScoreUI();
}
public void DecreaseScore(int score)
{
this.score -= score;
this.UpdateScoreUI();
}
public void LoadGameOverScene()
{
SceneManager.LoadScene("GameOverScene");
}
}