Instantiating as child of raycast

Hello, looked through the forum and found some solutions that were close to what I want, but not quite. I’m trying to parent something instantiated, and make it a child of the object hit by the raycast. Like an arrow or bullethole sticking to something that can move. I’ve got

Instantiate(Bullet_Default,shootHit.point,Quaternion.FromToRotation(Vector3.up,shootHit.normal));

and that gives me a bullet hole on the surface of the object, but my next step is to figure out how to get it to move with the object it hit, so it doesn’t leave a wall of bullet holes behind if the object moves. Sorry to be filling up the forum with questions that are so basic

Thought I had it figured using Bullet_Default.transform.SetParent(shootHit.transform);, but get an error message saying Setting the parent of a transform which resides in a prefab is disabled, thats if it even would have worked

Bullet_Default is the prefab you’re ‘cloning’ as a result of the Instantiate. Just use what’s returned from that method (which is the clone itself) and set its parent instead.

var clone = Instantiate(...);
clone.transform.parent = shootHit.transform;

Thanks! Unfortunately following that gives me the error : error CS0117: UnityEngine.Object does not contain a definition for transform.

Put “(GameObject)” in front of Instantiate(). Or “as GameObject;” after it. When you specify a position and rotation to Instantiate it returns an Object, not a GameObject, so you must cast.

Or even better, use the generic overload of Instantiate

var clone = Instantiate<GameObject>(......);
1 Like

I was not aware of that!