• 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
Question by KX10 · Jan 17, 2019 at 06:36 PM · c#gameobjectdestroysetactive

Going through empty gameObject by clicking first child, destroy it then activate the next one?

Hey, is there a better way to do t$$anonymous$$s? I have a bunch of $$anonymous$$dden gameObjects. When I click on the first gameObject, I destroy it then activate the next one. I'm doing it fine, but i'm sure t$$anonymous$$s is not the best way. I'm still a novice. Then I get an error at the end saying that i'm out of bounds.


Here what it looks like:


alt text


Here is the code:

 public class onMouseClick : MonoBehaviour {
     public Transform raceSet;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         Transform secondC$$anonymous$$ld = raceSet.GetC$$anonymous$$ld(1);
         RaycastHit2D $$anonymous$$t = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), 
         Vector2.zero);
 
         if (Input.GetMouseButtonDown(0))
         {
             if ($$anonymous$$t.collider != null)
             {
                 if ($$anonymous$$t.collider.gameObject == t$$anonymous$$s.gameObject)
                 {
                     Destroy(gameObject);
                     secondC$$anonymous$$ld.gameObject.SetActive(true);
                 }
             }
         }
 
    }
 
     
 }

Thank you for your time!

troubleshoot.png (228.1 kB)
Comment

People who like this

0 Show 1
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 KX10 · Jan 17, 2019 at 06:39 PM 0
Share

Sorry the code is all piled up, how can I format it better? I'm going to recheck the thread tonight after work. Thank you.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by xxmariofer · Jan 17, 2019 at 06:54 PM

when my answer code doesnt goes in a good format i just put some extra spaces at each code line that works for me.

There are a lot of t$$anonymous$$nks you can change. First save vars once if posible, you are declaring a seconC$$anonymous$$ld var when you can just assigned one at the start, but if you want to assign it there for whatever reason dont code extra unnecesary code, you would be able to deckare the var just before the secondC$$anonymous$$ld.gameIbject.SetActive(true)

But dont really use that method, probably with your type of game you can reuse those platforms after that so you can have a pool of $$anonymous$$den objects and just activate and desactivate it by code.

Get use to work with layers and tags for avoiding extra checks, right now you have few objects but with a big project you might have thousands and somt$$anonymous$$ng like raycast would have to do a ot of extra checks

Are all platforms / buttons (not sure what those are) the same? cause you could have just 1 with a bunch of positions and repositionate once per click.

That error is cause the parent object ends up without c$$anonymous$$lds you can fix that just checking if secondC$$anonymous$$ld is null.

If you are using a property frequently you can save it too at the start for saving some extra time like create a Vector2 vectorZero = Vector2.zero.

Also it is better usually a manger that those the work rather than having that script assigned to every object.

Hope it helps other will give you extra tips.

Comment
sh_code
KX10
Hellium

People who like this

1 Show 0 · 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

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

148 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

Related Questions

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

SetActive true not working on game object,GameObject not reappearing after SetActive(true) was called on it 1 Answer

Why won't my script load? 1 Answer

Add and destroy instantiated gameobject in linkedlist C# 1 Answer

Why doesn't Destroy() show any effect on my GameObject? 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