- Home /
Trail prefab as the child of existing object.
Hello, developers. I tried to make a trail of an explosion, which would apper in the point of collision and would be child to the object which was hit. My code works if and only if I implement it with primitive object. Once I try to put a prefab or a model manually, then I get either "object reference not set to an instance of an object" or "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption".
Here are two codes.
With primitives, works:
void OnCollisionEnter (Collision collision){ if (collision.gameObject.name == "Sphere(Clone)") { ContactPoint contactPoint = collision.contacts[0]; GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = contactPoint.point; cube.transform.rotation = Quaternion.FromToRotation(Vector3.up, contactPoint.normal); cube.transform.parent = transform; } }
With prefab (g1), gives "object reference not set...":
void OnCollisionEnter (Collision collision){ if (collision.gameObject.name == "Sphere(Clone)") { ContactPoint contactPoint = collision.contacts[0]; GameObject cube = Instantiate(g1, contactPoint.point, Quaternion.FromToRotation(Vector3.up, contactPoint.normal)) as GameObject; cube.transform.parent = transform; } }
Answer by Benedetto · Jun 30, 2014 at 01:05 AM
JUST FOUND THE SOLUTION! Probably somebody will need this:
void OnCollisionEnter (Collision collision){
if (collision.gameObject.name == "Sphere(Clone)") {
ContactPoint contactPoint = collision.contacts[0];
Transform cube = Instantiate(g1, contactPoint.point, Quaternion.FromToRotation(Vector3.up, contactPoint.normal)) as Transform;
cube.parent = transform;
}
}
Your answer
Follow this Question
Related Questions
C# Ignore Collision Trigger From Child 2 Answers
collision on child only returns parent object ? 1 Answer
Distribute terrain in zones 3 Answers
Attaching objects on collision 1 Answer
Multiple Cars not working 1 Answer