- Home /
Having trouble understanding parenting with componants
Alright so I'm having a hard time getting things to work with parenting due to one reason, I can't wrap my head around the way it works. It's just so seemingly inconsistent that I can't figure it out. The way it changes the way get component works seems to be different every time. Alright so I have two objects with the "MobStats" script on them. The MobStats script handles mob health.
One of these objects is the child of another object (you have to shoot a specific part of him to damage him), the other is the parent of 2 other objects. I also have 2 ways of calling the damage function on the MobStats script:
void OnTriggerEnter (Collider Hit){
HitTransform = Hit.transform;
Debug.Log(HitTransform.gameObject);
if (HitTransform.GetComponent<MobStats>())
{
DamageMob = HitTransform.GetComponent<MobStats>();
DamageMob.DamageMob(Damage);
}
}
and
void OnCollisionEnter (Collision WasHit){
HitByCannon = WasHit.transform;
Debug.Log(HitByCannon.gameObject);
if (HitByCannon.tag == "Enemy")
{
DamageMob = HitByCannon.GetComponent<MobStats>();
DamageMob.DamageMob(Damage);
}
Destroy(gameObject);
}
They both use the transform of whats it. This is where I start to get confused, the OnTriggerEnter script (ball that expands until it takes up the whole level and should hit everything) just flat out wont hit either of the two objects although 1/10 times it seems to hit every part of the object instead. Debug comes up with no object, or every object in the hierarchy. When I put the part with the MobStats script on it's own, it hits it every time. The only thing more confusing than this is how my OnCollisionEnter works.
My gun fires a small projectile with the OnCollisionEnter script on it and if it hits the object where MobStats is on the parent, then it will debug the parent object every time no matter what it hits in the hierarchy. However if it hits the object where MobStats is in the child object, it will debug the child object every time meaning I get an error if it hits any part of the object without the script as it still tries to access it. I could check if the object has the script but that's besides the point.
Is anyone able to explain why it seems to act in a different way everytime? Is there a way to do something similar to parenting that doesn't change the way get component and collisions works?
Your answer
Follow this Question
Related Questions
Stop moving when colliding. 2D 1 Answer
Multiple Colliders With Mechanim 1 Answer
How can I get Compound Colliders to work properly? 2 Answers
How to detect child object collisions on parent 3 Answers
How to activate parent camera of gameobject -1 Answers