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
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 : $$anonymous$$onoBehaviour
{
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 column$$anonymous$$in = -1f; //$$anonymous$$inimum y value of the column position.
public float column$$anonymous$$ax = 3.5f; //$$anonymous$$aximum 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(column$$anonymous$$in, column$$anonymous$$ax);
//...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;
}
}
}
"
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.
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
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.
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
Answer by arrann · Mar 04, 2020 at 05:28 PM
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;
I am not using a mesh Renderer to be honest am i supposed to?
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
yeah i was unable to use a mesh renderer since i was using a sprite renderer
Your answer
Follow this Question
Related Questions
How can I destroy a prefab clone? 1 Answer
Destroy a spawning enemy prefab with an weapon prefab 0 Answers
Destroying enemy only partially works 0 Answers
Destroying a spawned prefab instance 0 Answers