using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class BasketsController : MonoBehaviour
{
[SerializeField]
private List<GameObject> baskets;
[SerializeField]
private Button btnStartGame;
public GameObject buttonGo;
// Start is called before the first frame update
void Start()
{
this.btnStartGame.onClick.AddListener(() =>
{
SceneManager.LoadScene("GameScene");
});
buttonGo = GameObject.Find("Button");
buttonGo.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float maxDistance = 10f;
Debug.DrawRay(ray.origin, ray.direction * maxDistance, Color.green, 1f);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxDistance))
{
GameObject foundBasketGo = this.baskets.Find(x => x == hit.collider.gameObject);
buttonGo.SetActive(true);
//int index = -1;
//for(int i = 0; i < this.baskets.Count; i++)
//{
// if (foundBasketGo == this.baskets[i])
// {
// index = i;
// break;
// }
//}
int selectedBasketType = this.baskets.IndexOf(foundBasketGo); //찾아주는 메서드
InfoManager.instance.selectedBasketType = selectedBasketType;
foreach(var go in this.baskets)
{
if (go != foundBasketGo)
{
go.SetActive(false);// 비활성화
}
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InfoManager
{
//싱글톤
public static readonly InfoManager instance = new InfoManager();
public int selectedBasketType = -1;
private InfoManager()
{
}
}
using Palmmedia.ReportGenerator.Core.Reporting.Builders;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
[SerializeField]
private Text txtTime;
[SerializeField]
private Text txtScore;
private float time = 60.0f;
private int score = 0;
// Start is called before the first frame update
void Start()
{
this.UpdateScoreUI();
}
// Update is called once per frame
void Update()
{
this.time -= Time.deltaTime;
this.txtTime.text = this.time.ToString("F1");//소수점 1자리까지 표시
}
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();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemGenerator : MonoBehaviour
{
private float elasedTime;//경과시간
private float spawnTime = 1f; //1초에 한번씩
public GameObject applePrefab;
public GameObject bombPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//시간 재기
//변수에 Time.deltaTime을 더해라
this.elasedTime += Time.deltaTime;
if (this.elasedTime >= this.spawnTime)
{
this.CreateItem();
//초기화
this.elasedTime = 0;
}
}
private void CreateItem()
{
//사과 똫는 폭탄
int rand=Random.Range(0, 11);// 1~10사과가 나오는 횟수를 더 늘리고싶음
GameObject itemGo = null;
if (rand>3)
{
itemGo= Instantiate<GameObject>(this.applePrefab);
}
else
{
itemGo= Instantiate<GameObject>(this.bombPrefab);
}
//위치설정
//x:-1,1
//Z:-1,1
int x= Random.Range(-1, 2);//-1,0,1
int z = Random.Range(-1, 2);
itemGo.transform.position=new Vector3(x, 3.5f, z);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemController : MonoBehaviour
{
[SerializeField]
public float moveSpeed = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//translate는 기본이 로컬좌표로 이동이다
this.transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);//(0,-1,0)
if (this.transform.position.y <= 0)
{
Destroy(this.gameObject);//Destroy(this)는 완전히 다른의미이다.
}
}
}
버튼을 비활성화 클릭하고 시작하니까 계속 nullreference 오류가 났음--> 해결 시작할때 setActive(false)로 하고 ray에 충돌있을 때 setActive(ture)로 하니 해결됨

'유니티 기초' 카테고리의 다른 글
| 게임오버씬 (0) | 2023.08.07 |
|---|---|
| 선택된 바구니 게임씬에서 가져오기 (0) | 2023.08.07 |
| 주말과제(수정) (1) | 2023.08.04 |
| 클릭한 위치로 캐릭터 이동 (0) | 2023.08.04 |
| 캐릭터 타겟을 향해 대각선으로 움직이기 (0) | 2023.08.04 |