- Home /
If A collide with B A will become Child of B - C#
Hi guys,
I'm working on a arrow type projectile, everything is fine but the projectile doesn't become child to the object that hit. Can someone tell me what i'm doing wrong? Thanks
Arrow.cs
using System.Collections;
public class Arrow : MonoBehaviour
{
public GameObject StickyProjectile;
public GameObject hitEffect;
public float Damage;
void OnHit()
{
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
Instantiate(hitEffect, transform.position, randomRotation);
Instantiate(StickyProjectile, transform.position, transform.rotation);
Destroy (gameObject);
}
void OnTriggerEnter2D (Collider2D col)
{
//on collision this gameObject will become a child of the collided object before destroying it
transform.parent = col.transform;
if(col.tag == "Enemy")
{
col.gameObject.GetComponent<EnemyHp>().HP -= Damage;
OnHit();
Destroy(gameObject);
}
else if(col.gameObject.tag == "Breakable")
{
col.gameObject.GetComponent<BreakablePropScript>().PropHp -= Damage;
OnHit();
Destroy(gameObject);
}
else if(col.gameObject.tag != "Player")
{
OnHit();
Destroy(gameObject);
}
}
}
Answer by robertbu · Apr 30, 2014 at 02:41 AM
I'm sure line 26 works just fine. Unfortunately, you delete this child object (Arrow) in the same frame. My guess is that what you really want to do is to make the StickyProjectile the child. Pass the parent to your OnHit() method:
void OnHit(Transform parent)
{
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
Instantiate(hitEffect, transform.position, randomRotation);
GameObject go = Instantiate(StickyProjectile, transform.position, transform.rotation) as GameObject;
go.transform.parent = parent;
Destroy (gameObject);
}
Then you would just call it:
OnHit(col.transform);
As a side note, since you have a 'Destory(gameObject)' as the last line of your OnHit(), you don't need it on lines 33, 39 and 44.
Answer by Jeff-Kesselman · Apr 29, 2014 at 09:27 PM
Well, in all cases you are destroying the hit object so Im not sure how it could become the parent of anything.
Separately, I see no parenting code there whatsoever.
Before destroying the arrow it will create the "StickyProjectile" inside the collided object and the parenting code is in line 26. I don't understand why the line 26 didn't work.
Answer by cansit · Apr 30, 2014 at 09:39 AM
Thank you guys solved! Here's the full code so it can be helpful to other people.
Arrow.cs
using System.Collections;
public class Arrow : MonoBehaviour
{
//This script must be attached to the projectile prefab.
public GameObject StickyProjectile;
public GameObject hitEffect;
public float Damage = 25f;
void OnTriggerEnter2D (Collider2D col)
{
if(col.tag == "Enemy")
{
col.gameObject.GetComponent<EnemyHp>().HP -= Damage;
OnHit(col.transform);
}
else if(col.gameObject.tag == "Breakable")
{
col.gameObject.GetComponent<BreakablePropScript>().PropHp -= Damage;
OnHit(col.transform);
}
else if(col.gameObject.tag != "Player")
{
OnHit(col.transform);
}
}
void OnHit(Transform parent)
{
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
Instantiate(hitEffect, transform.position, randomRotation);
GameObject go = Instantiate(StickyProjectile, transform.position, transform.rotation) as GameObject;
go.transform.parent = parent;
Destroy (gameObject);
}
}
Your answer
Follow this Question
Related Questions
How to make a projectile fly towards it's target 2 Answers
Model rotation and hierarchy of game objects - not rotating! 1 Answer
Setting a Transform or GameObject SyncVar from a Command 0 Answers
Offset transform.parent.position - Help 1 Answer
Accessing children of instances vs children of original prefab 1 Answer