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);
            		}
            		
            	}
    
}

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.

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.

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);
		}
}