• 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 /
  • Help Room /
avatar image
Question by carmanroofer93 · Mar 02, 2020 at 12:22 PM · destroyprefabsrespawnobjectpoolcollectible-game-objects

destroying a collected coin

okay guys so i am pretty new, and im stumped a bit,m basically using everything from the flappy bird tutorial mechanics.. anyways so here are the details. i have coins on the screen, ive managed to give them sound effects awesome, okay so i need them to disappear... the only thing ive tried that works is "

 using UnityEngine;
 using System.Collections;
 
 public class Column : MonoBehaviour
 {
     **void OnTriggerEnter2D(Collider2D other)
     {
         if (other.GetComponent<Bird>() != null)
         {
             //If the bird hits the trigger collider  then
             //tell the game control that the bird scored.
             GameControl.instance.BirdScored();
             Destroy(gameObject);
         }**
     }
 }

" now i understand that destroys everything including the script.. and my game is crashing because its trying to find these destroyed remains, anyways so i'm sure you already know my question now, but i will still ask it, what exact code do i need to basically say after the score increases make the object disappear until it needed again. (or needs to re-spawn etc) i've been trying to figure out the sound for days now finally got it, felt great when my coins started disappearing with a cha'ching sound but a keep running into walls here, and im running out of free time this is the last thing i need to polish off for now, so if anyone can help me out i would greatly appreciate it, last bits of details, i have 2 different coins, both in a separate prefab of their own, they are literally just single coins in the prefabs (except the one has 5 overlapping each other to give 5 poiints per coin. ) if my grammer and typing is bad i apologize im super exhausted and heading to bed now lol, thanks again

Comment
cupcake007

People who like this

1 Show 2
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 carmanroofer93 · Mar 02, 2020 at 11:03 PM 0
Share

I have been searching since i came home from work tried different methods and still nothing , i figure maybe adding my column pool script will help achieve an answer from one of you awesome folk, if anyone can help me out i will love you forever, thank you a million times. . ive searched the forums and cannot find anything that helps.

"

 using UnityEngine;
 using System.Collections;
 
 public class ColumnPool : MonoBehaviour
 {
     public GameObject columnPrefab;                                    //The column game object.
     public int columnPoolSize = 5;                                    //How many columns to keep on standby.
     public float spawnRate = 3f;                                    //How quickly columns spawn.
     public float columnMin = -1f;                                    //Minimum y value of the column position.
     public float columnMax = 3.5f;                                    //Maximum y value of the column position.
 
     private GameObject[] columns;                                    //Collection of pooled columns.
     private int currentColumn = 0;                                    //Index of the current column in the collection.
 
     private Vector2 objectPoolPosition = new Vector2(-15, -25);        //A holding position for our unused columns offscreen.
     private float spawnXPosition = 10f;
 
     private float timeSinceLastSpawned;
 
 
     void Start()
     {
         timeSinceLastSpawned = 0f;
 
         //Initialize the columns collection.
         columns = new GameObject[columnPoolSize];
         //Loop through the collection... 
         for (int i = 0; i < columnPoolSize; i++)
         {
             //...and create the individual columns.
             columns[i] = (GameObject)Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
         }
     }
 
 
     //This spawns columns as long as the game is not over.
     void Update()
     {
         timeSinceLastSpawned += Time.deltaTime;
 
         if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
         {
             timeSinceLastSpawned = 0f;
 
             //Set a random y position for the column
             float spawnYPosition = Random.Range(columnMin, columnMax);
 
             //...then set the current column to that position.
             columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
 
             //Increase the value of currentColumn. If the new size is too big, set it back to zero
             currentColumn++;
 
             if (currentColumn >= columnPoolSize)
             {
                 currentColumn = 0;
             }
 
            
         }
     }

"

avatar image carmanroofer93 · Mar 08, 2020 at 08:31 PM 0
Share

i finally fingured it out, if anyone wants to know how let me know and ill post the script, but its taken me over a week to figure out but i tell yah it feels damn good.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by carmanroofer93 · Mar 04, 2020 at 04:09 AM

update i have tried many different options that work however same results, i've looked on unity docs , here, stack overflow, other sources, and so many people have the same problem with different fixes due to obvious reasons such as different game different code/structure etc. however, is there a line of code to add to my object pooling script to make the coins disappear, i managed to make the coins disapear set.active false but one all the clones are set to false there\s no way for me to set them true again, even with adding set.active true underneath. and other possible locations. its been a week, i don't like posting questions online i just like to figure stuff out on my own but i sucked in my pride to ask for help and i'm not receiving anything and im frustrated lol please people

Comment

People who like this

0 Show 2 · 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 cupcake007 · Mar 04, 2020 at 06:18 AM 0
Share

I'm guessing that you're instantiating a prefab of the coin as multiple clones. You could set the name of each clone to a count and ++ it every time you create another coin. Like: coin.name = "coin" + count;
then call the coin by it's name or call a batch of them with a for loop.

avatar image carmanroofer93 cupcake007 · Mar 07, 2020 at 06:44 PM 0
Share

i appreciate your reply , i am just seeing this now i took a break hoping id have success after giving it some time, i am instantiating under a column pool script which continues to spawn them over the screen which makes the clones, I've managed to destroy the (gameobjects) which destroyed the script, and resaulted with trying to instantiate a destroyed gameobject, then ive tried different ways of set.active false followed with multiple various locations for set.act true including underneath set.active false, but no matter what it they disable each clone until none show up on screen, they are there, they are just disabled, im curious on your answer, i will look in case i find it before you reply but how would i go about that exactly? i can change the coin count and what not but how do i recall them with a loop? how would that work like, which script would i be editing as well, the script that controls my pooling or the script that controls adding the score on collision with entertrigger2d. i have an idea but its cloudy

avatar image

Answer by arrann · Mar 04, 2020 at 05:28 PM

@carmanroofer93

I'm new here too so sorry if this doesnt work, but how about disabling the mesh renderer of the coins so they're just invisible, and enable them again when needed

 public MeshRenderer mr;
 
 mr.enabled = false;
 mr.enabled = true;
Comment

People who like this

0 Show 3 · 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 carmanroofer93 · Mar 07, 2020 at 06:34 PM 0
Share

I am not using a mesh Renderer to be honest am i supposed to?

avatar image carmanroofer93 carmanroofer93 · Mar 07, 2020 at 06:46 PM 0
Share

i am going to try and add a mesh renderer and adding a script just for it.. thank you, i will let you know how it goes

avatar image carmanroofer93 · Mar 08, 2020 at 02:48 PM 0
Share

yeah i was unable to use a mesh renderer since i was using a sprite renderer

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

210 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 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 avatar image avatar image

Related Questions

Destroying enemy only partially works 0 Answers

References to Prefab get lost on start 1 Answer

How can I destroy a prefab clone? 1 Answer

Destroy a spawning enemy prefab with an weapon prefab 0 Answers

I created a script what controls ammo, but after my player dies and respawns, the pistol refuses to reload the ammo when it reaches zero 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