Instantiating into a hierarchy

I want to instantiate a prefab so that it becomes a child of the instantiating transform, i.e. attached to it.
Any ides? It’s to be used for things like arrows getting stuck in characters, equipment changes etc.

 newObject = Instantiate(prefab, targetParent.position, targetParent.rotation);
newObject.transform.parent = targetParent;

for arrows other projectiles there’s this script as well:

http://forum.unity3d.com/viewtopic.php?t=1597

Hey guys,
How do you actually implement this? Heres the snippet from the scripting tutorial mixed with Yoggys bit. Im sure its simple but I havent absorbed this yet.:

 var newObject : Transform; 
function Update () { 
? if (Input.GetButtonDown("Fire1")) { 
?    Instantiate(newObject, transform.position, transform.rotation); 
? 
newObject = Instantiate(prefab, targetParent.position, targetParent.rotation); 
newObject.transform.parent = targetParent; }
 }

So the instantiated prefab needs to parent to the thing it collided with, on collision…
Thanks guys
AC

That code won’t work at all for what you are trying to achive.

The first step is to instantiate the projectile when the player shoots.

var projectilePrefab : GameObject;
var gunTransform : Transform;

function Update ()
{
	if(Input.GetButtonDown("Fire1"))
	{
		projectile = Instantiate(projectilePrefab, gunTransform.position, gunTransform.rotation);
		// maybe set some parameter on the projectile like it's velocity if you use
		// rigidbodies for bullets.

		// projectileComponent = projectile.GetComponent(SomeCoolComponent);
		// projectileComponent.awesomeValue = "Something!!!";
	}
}

Then if it were me I would use another script to control the projectile. My favorite method is to use careful raycasts plus whatever else you want your projectile to do.

// this will collide with any collider not on IgnoreRaycast in a ray style fashion.
//If the explosion is directional make sure the "up" direction is (0, 0, 1) or forward.
// if you want the explosion to "bounce" off of the surface add particle velocity on (0, 1, 0) or up.
// to get damage working write a script that has a function called "Damage" that accepts a float parameter for how much damage delt.
//then attach that script to the collider you want to be damageable.
// you can also use multiple damage collider scripts and one damage "reciever" 
// any attached rigidbody of the collider hit will be pushed an amount of hitForce in a "realistic" fashion

var speed = 0.00;
var gravity = 0.00;
var acceleration = 0.00;
var drag = 0.00;
var damage = 0.00;
var hitForce = 0.00;
var explosion : GameObject;

private var velocity : Vector3;
private var hit : RaycastHit;

function Start ()
{
	velocity = transform.forward * speed;
} 

function Update ()
{
	transform.rotation = Quaternion.LookRotation(velocity);

	velocity.y -= gravity * Time.deltaTime;
	velocity -= velocity * drag * Time.deltaTime;
	velocity += transform.forward * acceleration * Time.deltaTime;

	distance = velocity.magnitude * Time.deltaTime;

	if(Physics.Raycast(transform.position, velocity, hit, distance))
	{
		Hit();
	}
}

function Hit ()
{
	transform.position = hit.point;

	other = hit.collider.gameObject;
	other.SendMessage("Damage", damage, SendMessageOptions.DontRequireReceiver);

	if(explosion)
	{
		rotation = Quaternion.LookRotation (hit.normal, transform.forward);
		Instantiate(explosion, hit.point, rotation);

		// alternately to stick the explosion on what it hit:
		// newExplosion = Instantiate(explosion, hit.point, rotation);
		// newExplosion.transform.parent = other.transform;
	}

	// Here is the important part for arrow sticking
	// find a child named "arrow" and stick it in the collider. this "arrow" should be a renderer object.
	arrow = transform.FindChild("arrow");
	if(arrow)
	{
		arrow.parent = other.transform;
	}

	if(hit.rigidbody  !hit.rigidbody.isKinematic) hit.rigidbody.velocity += (-hit.normal + velocity.normalized) * hitForce * 0.5;

	Destroy(gameObject);
}

Thanks for the tips. It gave me the clues I needed.