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 $$anonymous$$ · Aug 09, 2018 at 11:21 AM · functioncallchild-to-gameobject

how to use transform.GetChild(0) in a function ?

so I'm able to find children of a gameobject by their index number with above function, but I dont know how to call a particular child by their index number.
like I want to do something like this
if childA is disabled, enable childB
if childB is disabled, enable childC
if childC is disabled, and there is no childD, enable childA again
and the loop goes on forever
also I'm noob at programing, so if you could explain your function, that would be very helpful, I want to learn, not just copy paste.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by FernandoHC · Aug 09, 2018 at 11:38 AM

I'm actually having a really hard time understanding what you want to do. So I will assume you only want to enable objects, this is what you want then: for iterates through all childs, then checks if the current child is disabled, if so enables the next one.

 public void OnTargetHit()
 {
     gameObject.SetActive(false);
     if (transform.GetSiblingIndex() < transform.parent.childCount - 1)
         transform.parent.GetChild(transform.GetSiblingIndex() + 1).gameObject.SetActive(true);
     else
         transform.parent.GetChild(0).gameObject.SetActive(true);
 }
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 $$anonymous$$ · Aug 09, 2018 at 12:15 PM 0
Share

hi sorry english is not my first language. let me explain again.
so there's a bullet that when hits a target(1st child of empty gameobject),
that 1st target disappears and 2nd target appears, when bullet hits 2nd target it disappears and 3rd target appears. when last target disappear and there are no further targets left to shoot, 1st target appears again.
am I being clear now?

avatar image FernandoHC $$anonymous$$ · Aug 09, 2018 at 12:39 PM 0
Share

Ok, I have updated the code, you should put that on the bullet detection part of your code, on the child/target object.

avatar image $$anonymous$$ FernandoHC · Aug 09, 2018 at 01:39 PM 0
Share

yep, that worked beautifully. thanks. :).

avatar image
1

Answer by Bunny83 · Aug 09, 2018 at 12:31 PM

Ok based on your comment your question has completely changed and is much clearer now ^^. Things like that shouldn't be handled at a central point because you have to keep track of many things at the same time. Also checking if an object is disabled every frame is just bad design.


Instead you can create a very simply script which you attach to each target.

 // Target.cs
 using UnityEngine;
 
 public class Target : MonoBehaviour
 {
     public GameObject Next;
     private void OnCollisionEnter(Collision collision)
     {
         gameObject.SetActive(false);
         if (Next != null)
             Next.SetActive(true);
     }
 }


Just disable all targets in the scene except the first one. Now you can link your targets together in whatever order you like by dragging the "next" target which should be active onto the "Next" field in the inspector. You can also link the last target simply back to your first one.


If you don't want to do the linking by hand you can create another script on the parent which just does the linking for you:

 void Start()
 {
     if (transform.childCount <1)
     {
         Debug.LogWarning("not enough children to link them");
         return;
     }
     GameObject last = transform.GetChild(0).gameObject;
     for(int i = transform.childCount-1; i >= 0; i--)
     {
         Target target = transform.GetChild(i).GetComponent<Target>();
         if (target != null)
         {
             target.Next = last;
             last = target.gameObject;
         }
     }
 }

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 $$anonymous$$ · Aug 09, 2018 at 01:13 PM 0
Share

hi thanks for the comment.
your 2nd script for parent shows an error for (GameObject last = transform.GetChild(0);) cannot convert gameobject into transform
for (target = last;) can not convert gameobject into target.
and your 1st script, I manually assigned all the targets, it makes 1st target disappear when bullet hits, but 2nd target doesnt reappear

avatar image Bunny83 $$anonymous$$ · Aug 09, 2018 at 01:34 PM 0
Share

$$anonymous$$y bad, there where two small errors ^^. I've edited my answer. The changed lines are:

     GameObject last = transform.GetChild(0).gameObject;
     for(int i = transform.childCount-1; i >= 0; i--)

and

     target.Next = last;



Note the .gameObject at the end as well as the childCount-1 in the for loop. After the target i was missing the .Next. I've written this just quick and dirty here ^^.


In order for the first script to activate the next, the "Next" field has to be linked to the next target object. As i said you can do this in the inspector manually by dragging the next target which should be activated onto the Next field, or use the second script (which hopefully works now ^^).

avatar image $$anonymous$$ Bunny83 · Aug 09, 2018 at 04:21 PM 0
Share

hi, I tried your script now, it works perfect. but I do have to tell you that script mentioned by fernando is a litter better ;) so I used his method, hope that doesnt upset you. thank you for answering my question :)

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

90 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

Related Questions

Rpc client issue 0 Answers

Calling a function from a different script on the same object 1 Answer

Need help with If/else statement. 1 Answer

How to reference another script and call a function in C# ? 2 Answers

Check if a function is no longer being called? 3 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