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 /
  • Help Room /
avatar image
0
Question by PaolaGS · Sep 25, 2015 at 10:33 PM · gameobjectprefabslayers

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · 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 PaolaGS · Sep 28, 2015 at 05:26 PM 0
Share

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.

avatar image vladrybak PaolaGS · Sep 28, 2015 at 05:52 PM 0
Share

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;
     ...
avatar image PaolaGS vladrybak · Sep 29, 2015 at 05:16 PM 0
Share

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

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

31 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

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


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