- Home /
Destroy Without A Collision Or Trigger
Let me be clear... this script is attached to an 'invisible' wall in the center of the screen that triggers coins to instantiate. Ideally, I'd like the coins to disappear (Destroy) a few seconds after they instantiate. My code is not working, and for the life of me, I can't find a solution. I get an error that says 'MissingReferenceException: The variable coin of RandomCoins doesn't exist anymore. You probably need to reassign the coin variable of the 'RandomCoins' script in the inspector.'
I just can't, for the life of me figure out a workaround.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class RandomCoins : MonoBehaviour {
public GameObject[] spawnPoints;
public GameObject coin;
private int count;
public float timer;
private void Start()
{
count = 0;
}
void SpawnCoin()
{
int spawn = Random.Range(0, spawnPoints.Length);
GameObject.Instantiate(coin, spawnPoints[spawn].transform.position, Quaternion.identity);
StartCoroutine(MyCoroutine(timer));
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Ball")
{
count++; // Counting the number of times the meteor passes through the invisible wall in center of screen
if (count % 2 == 0)
{
SpawnCoin();
}
}
}
IEnumerator MyCoroutine(float timer)
{
yield return new WaitForSeconds(timer);
if (coin != null)
{
Destroy(coin);
}
}
}
Please help me. It's important that I figure this out. I'm presenting to investors in less than a month. This has been taking up too much of my time. I've been working diligently to figure this out. I've been looking up questions about destroying objects but all the questions I've seen only relate to an object being destroyed on collision or on trigger. This is neither. All I'm really doing is using 'Destroy(coin)' on a timer using IEnumerator, however, it's not working with results I just showcased. I'd appreciate anyone's expertise. Thank you in advance.
Answer by ranch000 · Oct 24, 2018 at 04:33 AM
You are destroying your main coin GameObject. You should destroy the Instatiated ones. In SpawnCoin() maintain a (list of) reference when you instantiate coin and destroy that reference in the coroutine.
Answer by latsushi · Oct 24, 2018 at 11:49 PM
@ranch000 Here's my revised code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class RandomCoins : MonoBehaviour
{
public GameObject[] spawnPoints;
public GameObject coin;
public GameObject someGameObject;
public float timer;
private int count;
private void Start()
{
count = 0;
}
void SpawnCoin()
{
int spawn = Random.Range(0, spawnPoints.Length);
GameObject.Instantiate(coin, spawnPoints[spawn].transform.position, Quaternion.identity);
someGameObject = GameObject.FindWithTag("Sprite Coins");
StartCoroutine(MyCoroutine(timer));
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Ball")
{
count++; // Counting the number of times the meteor passes through the invisible wall in center of screen
if (count % 2 == 0)
{
SpawnCoin();
}
}
}
IEnumerator MyCoroutine(float timer)
{
yield return new WaitForSeconds(timer);
Destroy(someGameObject);
}
}
It does work now. I'm curious if this is what you meant? I admit I'm not fully understanding what you're telling me.
Instantiate() returns the cloned Game Object. Therefore you do not have to find it again. Just do
someGameObject = GameObject.Instantiate(coin, spawnPoints[spawn].transform.position, Quaternion.identity);