When Optimus Prime was injured from the destruction of the old Autobot base, he was close to joining Alpha Trion in the AllSpark. Smokescreen revived Optimus by placing the Forge of Solus Prime on his hand. This prevented Optimus from joining the AllSpark and resurrected him in a new body.
Megatron was prevented by Unicron from entering the AllSpark. Unicron told Megatron that Unicron's blood was inside Megatron's body which prevented Megatron from joining the AllSpark. Unicron used Megatron's body as a vessel to attack Cybertron and nearly destroying the Core of Cybertron. Optimus trapped Unicron's anti-spark inside the vessel which once stored the AllSpark inside. In the end, Optimus Prime sacrificed himself to restore Cybertron's life.