Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Check not colliding when object has been destroyed,Check if object no longer colliding if it's been deleted 0 Answers

how to save destroyed state of an object 1 Answer

c# Using an IEnumerator yield WaitForSeconds to temporarily pause a While loop 3 Answers

Coroutine Not Working 2 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