• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by latsushi · Oct 24, 2018 at 02:06 AM · destroycoroutineienumeratordestroygameobject

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ranch000 · Oct 25, 2018 at 04:16 AM 0
Share

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);


Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy character controller and instantiate new one 1 Answer

How to Destroy Terrains (destroy(this.gameobject) not working) 1 Answer

Destroy 2D Objects when in contact 0 Answers

Destorying gameobjects on a network 0 Answers

Destroying clone object in list does not remove the first one 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges