Hello is there anyway i can destroy only one child of a gameobject?
This is my attempt but it does not work:
public bool GetChildren()
{
bool haveChildren = false;
if (gameObject.transform.childCount > 0)
{
haveChildren = true;
}
else if (gameObject.transform.childCount > 1)
{
DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
}
return haveChildren;
}
raarc
October 4, 2020, 7:47pm
2
Nilskam178:
Hello is there anyway i can destroy only one child of a gameobject?
This is my attempt but it does not work:
public bool GetChildren()
{
bool haveChildren = false;
if (gameObject.transform.childCount > 0)
{
haveChildren = true;
}
else if (gameObject.transform.childCount > 1)
{
DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
}
return haveChildren;
}
your code for destruction will never be reached, if childCount is larger than 1 it will also be larger than 0. Remove the “else” if you want it to work
Okey, im also wondering why my haveChildren equals true here but when i access it from another script it equals false. Can you help me awell
This is my access
void Update()
{
AimRotate aimRotate = gameObject.GetComponentInParent<AimRotate>();
if (aimRotate != null)
{
pickedUpGun = aimRotate.GetChildren();
}
ClickDetect();
}
I use print(haveChildren) and it shows true here but in my access its false
Here it shows true
public bool GetChildren()
{
bool haveChildren = false;
if (gameObject.transform.childCount > 0)
{
haveChildren = true;
}
if (gameObject.transform.childCount > 1)
{
DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
}
print(haveChildren);
return haveChildren;
}
Vryken
October 4, 2020, 11:04pm
4
Nilskam178:
Okey, im also wondering why my haveChildren equals true here but when i access it from another script it equals false. Can you help me awell
This is my access
void Update()
{
AimRotate aimRotate = gameObject.GetComponentInParent<AimRotate>();
if (aimRotate != null)
{
pickedUpGun = aimRotate.GetChildren();
}
ClickDetect();
}
I use print(haveChildren) and it shows true here but in my access its false
Here it shows true
public bool GetChildren()
{
bool haveChildren = false;
if (gameObject.transform.childCount > 0)
{
haveChildren = true;
}
if (gameObject.transform.childCount > 1)
{
DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
}
print(haveChildren);
return haveChildren;
}
Your order-of-operations are reversed here. You’re setting the “haveChildren” bool to true before destroying the child, when you should destroy the child first and then check if there are any children remaining:
public bool GetChildren() {
if(transform.childCount > 1) {
DestroyImmediate(transform.GetChild(1).gameObject);
}
return transform.childCount > 0;
}
This method doesn’t make much sense though, since if it’s only destroying one child if the transform has more than one child , then it will always return true 100% of the time, unless the transform has zero children to begin with.
Vryken:
Your order-of-operations are reversed here. You’re setting the “haveChildren” bool to true before destroying the child, when you should destroy the child first and then check if there are any children remaining:
public bool GetChildren() {
if(transform.childCount > 1) {
DestroyImmediate(transform.GetChild(1).gameObject);
}
return transform.childCount > 0;
}
This method doesn’t make much sense though, since if it’s only destroying one child if the transform has more than one child , then it will always return true 100% of the time, unless the transform has zero children to begin with.
Yep, it starts with no children so sometimes its false