* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
빗방울이 4개 떨어지고 나면 더이상 비가 떨어지지 않아요. 시간이 끝날 때까지 빗방울이 계속 떨어져야 하는데 꼭 4개 이후로는 콘솔창에 에러 메세지가 뜨면서 비가 안 와요. 코드를 읽어봤을 땐 아무 문제가 없는 것 같은데 실행을 해보면 문제가 있다고 그러니 해결 방법을 모르겠어요. 콘솔 창에 따르면 GameObject 개체가 파괴되었지만 계속 접근하려고 해서 그렇다는데 이게 무슨 뜻인지, 그리고 어떻게 해결해야 하는지 알려주실 수 있으신가요?
작성한 코드 및 에러 메세지
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject rain;
public GameObject panel;
public static GameManager I;
public Text scoretext;
public Text timetext;
int totalScore;
float limit = 60f;
void Awake()
{
initGame();
I = this;
}
// Start is called before the first frame update
void Start()
{
InvokeRepeating("makeRain", 0, 0.5f);
}
// Update is called once per frame
void Update()
{
limit -= Time.deltaTime;
if (limit < 0)
{
Time.timeScale = 0.0f;
panel.SetActive(true);
limit = 0.0f;
}
timetext.text = limit.ToString("N2");
}
void makeRain()
{
Instantiate(rain);
}
public void addScore(int score)
{
totalScore += score;
scoretext.text = totalScore.ToString();
}
public void retry()
{
SceneManager.LoadScene("SampleScene");
}
public void initGame()
{
Time.timeScale = 1.0f;
totalScore = 0;
limit = 60f;
}
}
rain.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rain : MonoBehaviour
{
int type;
float size;
int score;
// Start is called before the first frame update
void Start()
{
float x = Random.Range(-2.7f,2.7f);
float y = Random.Range(3.0f,5.0f);
transform.position = new Vector3(x, y, 0);
type = Random.Range(1,5);
if (type == 1)
{
size = 1.2f;
score = 3;
GetComponent<SpriteRenderer>().color = new Color(100 / 255f, 100 / 255f, 255 / 255f, 255 / 255f);
}
else if (type == 2)
{
size = 1.0f;
score = 2;
GetComponent<SpriteRenderer>().color = new Color(130 / 255f, 130 / 255f, 255 / 255f, 255 / 255f);
}
else if (type == 3)
{
size = 0.8f;
score = -5;
GetComponent<SpriteRenderer>().color = new Color(255 / 255.0f, 100.0f / 255.0f, 100.0f / 255.0f);
}
else
{
size = 0.8f;
score = 1;
GetComponent<SpriteRenderer>().color = new Color(150 / 255f, 150 / 255f, 255 / 255f, 255 / 255f);
}
transform.localScale = new Vector3(size,size,0);
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Ground")
{
Destroy(gameObject);
}
if (coll.gameObject.tag == "rtan")
{
Destroy(gameObject);
GameManager.I.addScore(score);
}
}
}
콘솔창 에러 메세지
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Instantiate[T] (T original) (at <e8a406da998549af9a2680936c7da25a>:0)
GameManager.makeRain () (at Assets/animation/script/GameManager.cs:44)