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 Satish004 · Aug 02, 2019 at 01:29 PM · child objectfindgameobjectswithtag

How To Find Child Object and set is own criteria??

I m working On 2d game...

i create a empty gameobject and store a multiple child object

i applid a different different tag on child game object but chile tag was not found???

Referance:

Game Object: ObstacleScript.cs

alt text

Obstacle Object :

alt text

MoveFast Object Image :

https://imgur.com/1pgChbV

Vertical Rotate Object Image :

https://imgur.com/a/jRD1rS5

Code:

Obstacle.cs // Put Script On GameObject

    void Update()
     {
         goTopToDown();
     }
 
     void goTopToDown()
     {
             //transform.position += Vector3.down * speed * Time.deltaTime;  

               //find with tag and move top to down object   ex: 8 object should be slow move top to down
             if(gameObject.tag== "obstacleobject")                     
             { 
                 transform.position += Vector3.down * speed * Time.deltaTime;            
                 Debug.Log("GameObject Name:" + gameObject.tag);
                 Debug.Log("inside vertical object");
             }

             //find with tag and Vertical Rotate object           //ex:  1 game  object should be rotate left to right
             if (gameObject.tag == "verticalrotateobject")  
             {
                 transform.Rotate(Vector3.forward, turnSpeed * rotatesped * Time.deltaTime);    
                 transform.position += Vector3.down  * speed * Time.deltaTime;                       //move top to down
                 Debug.Log("GameObject Name:" + gameObject.tag);
                 Debug.Log("inside vertical object");
             }

              //find with tag and move fast object        ex: 2 object is speed is very fast
             if (gameObject.tag == "movefastobject") 
             {
                 transform.position += Vector3.down * movefastobjectspeed * Time.deltaTime
                 Debug.Log("GameObject Name:" + gameObject.tag);
                 Debug.Log("inside movefast object");
             }
  }

  

   void OnTriggerEnter2D(Collider2D other)  // i want to when three tag  gameobject collided with my player then trigger fire
     {
             foreach (Transform child in transform)                      
             {
                 if (child.tag == "obstacleobject" || child.tag == "movefastobject" || child.tag == "verticalrotateobject") 
                 {
                     Debug.Log("chiildtag:" + child.tag);
                     gameovertext.SetActive(true);
                      
                     StartCoroutine(gameover());
                     Debug.Log("inside triggerfire");
                 }
         }



But issue is i not found my child tag???

obstacle-object.png (69.8 kB)
obstacle.png (72.5 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Vega4Life · Aug 02, 2019 at 02:28 PM

You are just checking if the parent object has the tag. Instead, you need to cycle through all the parents children and see if they have the tag. Something like this:


     private void goTopToDown()
     {
         foreach(Transform trans in transform)
         {
             if (trans.tag == "obstacleobject")
             {
 
             }
             else if (trans.tag == "verticalrotateobject")
             {
 
             }
             else if (trans.tag == "movefastobject")
             {
 
             }
         }
     }
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
avatar image
0

Answer by Yonal · Aug 02, 2019 at 02:27 PM

i made some modifications to your code try this

 void Update()
 {
     goTopToDown();
 }
 
 void goTopToDown()
 {
     //transform.position += Vector3.down * speed * Time.deltaTime;  
     //find with tag and move top to down object   ex: 8 object should be slow move top to down
     foreach (GameObject item in GetComponentsInChildren<GameObject>())
     {
         if (item.tag == "obstacleobject")
         {
             transform.position += Vector3.down * speed * Time.deltaTime;
             Debug.Log("GameObject Name:" + gameObject.tag);
             Debug.Log("inside vertical object");
         }
         //find with tag and Vertical Rotate object           //ex:  1 game  object should be rotate left to right
         if (item.tag == "verticalrotateobject")
         {
             transform.Rotate(Vector3.forward, turnSpeed * rotatesped * Time.deltaTime);
             transform.position += Vector3.down * speed * Time.deltaTime;                       //move top to down
             Debug.Log("GameObject Name:" + gameObject.tag);
             Debug.Log("inside vertical object");
         }
         //find with tag and move fast object        ex: 2 object is speed is very fast
         if (item.tag == "movefastobject")
         {
             transform.position += Vector3.down * movefastobjectspeed * Time.deltaTime
 
 
                  Debug.Log("GameObject Name:" + gameObject.tag);
             Debug.Log("inside movefast object");
         }
     }
 }
 
 void OnTriggerEnter2D(Collider2D other)  // i want to when three tag  gameobject collided with my player then trigger fire
 {
     foreach (Transform child in GetComponentsInChildren<Transform>())
     {
         if (child.tag == "obstacleobject" || child.tag == "movefastobject" || child.tag == "verticalrotateobject")
         {
             Debug.Log("chiildtag:" + child.tag);
             gameovertext.SetActive(true);
 
             StartCoroutine(gameover());
             Debug.Log("inside triggerfire");
         }
     }
 
 }

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 satish004004 · Aug 02, 2019 at 05:43 PM 0
Share

@yonal try this code but its give an error??? alt text

i dont know what i m missing??

error.png (97.5 kB)
avatar image Bunny83 · Aug 03, 2019 at 02:22 PM 0
Share

This doesn't make sense and can't work:

 GetComponentsInChildren<GameObject>())

The GameObject class is not a component. You could use the Transform component, however keep in $$anonymous$$d that GetComponentsInChildren will also get all nested objects as well, no matter how deep.

avatar image satish004004 Bunny83 · Aug 03, 2019 at 02:35 PM 0
Share

@Bunny83 thanks for your valueable suggastion i appriciate it but still my issue not solved??

i m facing this problem still

i m trying

  foreach (Transform item in GetComponentsInChildren<Transform>())   // i put this script parent object and parent is an empty game object parent have a rigidbody2d and child gameobject have static trigger collider 
   {
  
     } 

see referance : https://imgur.com/a/ozcJcO4

child gameobject not work as its own condition?? isssue iss herreeee

avatar image
0

Answer by Yonal · Aug 03, 2019 at 03:48 AM

try this

  void Update()
  {
      goTopToDown();
  }
  
  void goTopToDown()
  {
      //transform.position += Vector3.down * speed * Time.deltaTime;  
      //find with tag and move top to down object   ex: 8 object should be slow move top to down
      foreach (Transform item in GetComponentsInChildren<Transform>())
      {
          if (item.tag == "obstacleobject")
          {
              transform.position += Vector3.down * speed * Time.deltaTime;
              Debug.Log("GameObject Name:" + gameObject.tag);
              Debug.Log("inside vertical object");
          }
          //find with tag and Vertical Rotate object           //ex:  1 game  object should be rotate left to right
          if (item.tag == "verticalrotateobject")
          {
              transform.Rotate(Vector3.forward, turnSpeed * rotatesped * Time.deltaTime);
              transform.position += Vector3.down * speed * Time.deltaTime;                       //move top to down
              Debug.Log("GameObject Name:" + gameObject.tag);
              Debug.Log("inside vertical object");
          }
          //find with tag and move fast object        ex: 2 object is speed is very fast
          if (item.tag == "movefastobject")
          {
              transform.position += Vector3.down * movefastobjectspeed * Time.deltaTime
  
  
                   Debug.Log("GameObject Name:" + gameObject.tag);
              Debug.Log("inside movefast object");
          }
      }
  }
  
  void OnTriggerEnter2D(Collider2D other)  // i want to when three tag  gameobject collided with my player then trigger fire
  {
      foreach (Transform child in GetComponentsInChildren<Transform>())
      {
          if (child.tag == "obstacleobject" || child.tag == "movefastobject" || child.tag == "verticalrotateobject")
          {
              Debug.Log("chiildtag:" + child.tag);
              gameovertext.SetActive(true);
  
              StartCoroutine(gameover());
              Debug.Log("inside triggerfire");
          }
      }
  
  }
Comment
Add comment · Show 1 · 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 satish004004 · Aug 03, 2019 at 05:03 AM 0
Share

@yonal i m trying this also

void goTopToDown() { foreach (Transform item in GetComponentsInChildren()) { foreach (Transform item in GetComponentsInChildren()) { Debug.Log("Inside go");

             if (item.tag == "obstacleobject")
             {
                 transform.position += Vector3.down * speed * Time.deltaTime;                   //move top to down object
                 Debug.Log("GameObject obstacle object:" + item.tag);
                 Debug.Log("inside obstacle object");
                 
             }
             else if (item.tag == "verticalrotateobject")
             {
                 transform.Rotate(Vector3.forward, turnSpeed * rotatesped * Time.deltaTime);    //Vertical Rotate object

                 transform.position += Vector3.down * speed * Time.deltaTime;                       //move top to down

                 Debug.Log("GameObject vertical rotateobject:" + item.tag);
                 Debug.Log("inside vertical object");
             }
             else if (item.tag == "movefastobject")
             {
                 transform.position += Vector3.down * movefastobjectspeed * Time.deltaTime;     //move fast object
                 Debug.Log("GameObject movefast object:" + item.tag);
                 Debug.Log("inside movefast object");
             }
           
 https://imgur.com/a/ozcJcO4    

what should i m missed in my program??

still not solve solve my isssue

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

112 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

Related Questions

Finding child gameobjects with findgameobjectwithtag 1 Answer

Order of GameObject.FindGameObjectsWithTag(string tag) 3 Answers

Spawner control and ending the level 0 Answers

How can I find all objects in scene under tag "Pickup"? 1 Answer

Script finding a none-existing player clone 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