Target appearing and disappearing
I want to make objects ( in this case targets ) to appear on the screen and disappear after a certain time, however I want to be able to click on them to make them disappear as well. I ran in to a problem that if I destroy the object that the script has spawned and do it fast enough so that the spawner can't produce enough targets for me to click, it shows an error that there's nothing to instantiate (so I destroy the targets faster than the spawner can spawn them). Here's the code that I've written so far. (To destroy the target I have a js script on the dots that spawn)
public GameObject dot; float randX; float randY; Vector2 whereToSpawn; public float spawnRate = 2f; float nextSpawn = 0f; public float lifetime;
void Update()
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-2f, 3f);
randY = Random.Range(0f, 8f);
whereToSpawn = new Vector2(randX, randY);
dot = Instantiate(dot, new Vector2(randX, randY), Quaternion.identity) as GameObject;
Destroy(dot.gameObject, lifetime);
THE ERROR:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your answer
Follow this Question
Related Questions
How to disappear and reappear an object by time (every "x" seconds)? 0 Answers
Javascript: Appearing and Disappearing objects 0 Answers
How do I make both an Image and Text appear when a button is clicked? 0 Answers
problem with timeScale = 0 (game paused) 0 Answers
i have a problem in admob i want to add interstiel after click but but i don't know plz help 0 Answers