- Home /
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
Obstacle Object :
MoveFast Object Image :
Vertical Rotate Object Image :
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???
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")
{
}
}
}
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");
}
}
}
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.
@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
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");
}
}
}
@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