My gameobjects keep changing layers
I have lots of prefabs gameObjects and I Instantiate them in a parent gameObject. When the user clicks one object, the prefab destroy. I have 48 of this prefabs in the scene. But when I click one object, the other ones keep moving back and forth, like changing layers or something. One or two seem to disappear but that object just move behind others.
Here is some of the code that I'm using:
this is how I instantiate the prefabs in my gameObject parent:
public GameObject[] prefabs;
private GameObject temporalPrefabs;
int index = Random.Range(0, 20);
temporalPrefabs= Instantiate(prefabs[index], transform.position, transform.rotation) as GameObject;
temporalPrefabs.transform.parent = transform;
and this is how I destroy the clicked object (this code is added as component in each prefab)
void OnMouseDown()
{
Destroy(gameObject);
}
if some one please know what could be happening and how can I fix this I'll be really thankful.
Answer by vladrybak · Sep 26, 2015 at 06:00 AM
Your code does not provide enought information. Try to Understand what's going on. Add a Debug Log on your prefab's Start and Destroy. You can use a less count of prefabs to test and provide a name to each prefab with it's index. You can Debug.log for a specific prefab by checking it's name. If you add a specific log to Update function you could check your object's lifetime and position. Destroy function remove object with some delay. Try to Use DestroyImmediate instead. It could be this case.
What I have is like a machine that drops different coins when touched, if the user slide the finger it does another action. I have 5 different coins prefabs. So the code that goes to the machine is:
public GameObject[] prefabs;
private GameObject temporalPrefabs;
void On$$anonymous$$ouseDown() {
//inicial position
posInicial = Input.mousePosition;
}
void On$$anonymous$$ouseUp() {
//final position
dropCoins = true;
posFinal = Input.mousePosition;
}
void Update(){
float total = posFinal.x - posInicial.x;
if(dropCoins && total == 0){
int index = Random.Range(0, 5);
temporalPrefabs= Instantiate(prefabs[index], transform.position, transform.rotation) as GameObject;
temporalPrefabs.transform.parent = transform;
GetComponent<Rigidbody2D>().AddForce(transform.right * 75f);
}
}
And in every prefab coin all the code that goes is (I already tried the DestroyImmediate and the problem was not solved):
void On$$anonymous$$ouseDown()
{
DestroyImmediate(gameObject);
}
So basically what I have is a lot of coins (in the heirarchy the name of the prefabs change to Clone) in the floor and when one coin is touched and destroys, some coins move behind other coins, and then the same coin move to the front. I already Debug.Log the position and does not change.
Update function is called every frame on every coin. How many coins instantiates when you destroy one? if only one must be instantiated maybe you should make dropCoins variable static and set to false after entered in instantiate condition? It's just a guess, because i can't see the whole picture...
if(dropCoins && total == 0){
dropCoins = false;
...
Yes, the dropCoins variable should be false like you said. But the problem was not solved that way. I realized that they where all in the same layer and the "order in layer" was the same for all, so they moved back and forth the way they liked. So I placed each coin in a different order in layer an that worked :) thanks for you time and help
Your answer
Follow this Question
Related Questions
ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer
How do i move a game object in front of another one 2D? 1 Answer
Survival Shooter Tutorial - Applying Layer causes model to disappear (SOLVED) 1 Answer
Prefabs not turning blue and losing connection 6 Answers
Instantiate more than 1 prefab 0 Answers