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