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 /
avatar image
0
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 this? I have a bunch of hidden 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 this 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 this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         Transform secondChild = raceSet.GetChild(1);
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), 
         Vector2.zero);
 
         if (Input.GetMouseButtonDown(0))
         {
             if (hit.collider != null)
             {
                 if (hit.collider.gameObject == this.gameObject)
                 {
                     Destroy(gameObject);
                     secondChild.gameObject.SetActive(true);
                 }
             }
         }
 
    }
 
     
 }

Thank you for your time!

troubleshoot.png (228.1 kB)
Comment
Add comment · 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
1

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 thinks you can change. First save vars once if posible, you are declaring a seconChild 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 secondChild.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 hiden 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 somthing 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 childs you can fix that just checking if secondChild 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
Add comment · 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.

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

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer

Cannot destroy Component while GameObject is being activated or deactivated 2 Answers

Particle system not destroying. 3 Answers

Store Game Object Into List For Later Reinstantiation? 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