- Home /
Parenting Bullet Holes?
I have a prefab object that is a plane with a bullet hole texture on it. I can shoot objects just fine and spawn bullet holes where my ray cast hits, but my bullet holes are not following the object that I shoot. I am not sure what to do. What I do know what to do is to use the lines of code in the given piece of code:
if(decalHitEnemy && hit.transform.tag == "Enemy")
{
Instantiate(decalHitEnemy, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
Instantiate(enemyHitParticle, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
Answer by Benproductions1 · Feb 20, 2013 at 03:41 AM
What you have to do is parent the bullet holes to the object they hit.
Example:
//Lets make the prefab a Transform for ease of use
var prefab:Transform;
// At the start of your script create a temporary variable
private var tmpObject:Transform;
//When you instantiate you set the return to that variable
tmpObject = Instantiate(prefab, /*your code here*/);
//You then Make what it hit, the parent
tmpObject.parent = hit.transform;
//If you'r prefab is of a type other than Transform, you will have to access the transform before you can apply the parent
tmpObject.transform.parent = hit.transform;
If your objects scale is changed, you will have to come up with a work around, but for now it will work
Hope this helps, Benproductions1
A quicker, more ugly way is to set it directly: Instantiate(/your code here/).parent = hit.transform; It might be a bit faster but more ugly code... your choice :)
I get an error when I shoot my enemy: $$anonymous$$issingFieldException: Field 'UnityEngine.GameObject.parent' not found. And I get this error when I use Transfrom: Assets/BulletScript.js(23,48): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.
Also either way the bullet hole won't parent.
What do I do?
$$anonymous$$ake sure you both typecast correctly. The answer is expecting a type of Transform to work, so it should be :
tmpObject : Transform = Instantiate(/*your code here*/);
tmpObject.parent = hit.transform;
in the answer, decalHitEnemy needs to be of type Transform. If decalHitEnemy is of type GameObject, use this :
var tmpObject : GameObject = Instantiate( decalHitEnemy, /*your code here*/);
tmpObject.transform.parent = hit.transform;
Your answer
![](https://koobas.hobune.stream/wayback/20220613093028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop textures flickering 2 Answers
Destroy Turret with machine Gun 0 Answers
How can I add a distance cap to this script? 3 Answers
Ricochet Problem of projectile bullets 0 Answers
EteeskiTutorials' bullets (raycast bullets with gravity) 1 Answer