0
\$\begingroup\$

I have made two prefabs of my enemies for my 2D game: normal and damaged. The I put several of these prefabs on scene. When enemies health <=0 I need to destroy its GameObject and then instantiate a damaged prefab on its position. But when I am doing this, all other prefabs are destroying too and damaged prefabs are instantiated on their positions. After searching this site I found out that any changes with one prefab are applying to all other prefabs on scene? Is this right?

P.S. I have edited the script where I am destroying prefab and added to it a simple check, if the prefabs renderer is visible on scene, then I need to destroy it, because all other prefabs are not visible at this moment. Do I need to check to make sure that this is the instance of the prefab is that I need every time when I want to apply to it some change?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Once a prefab has been instantiated, anything you do to that prefab should be independent of other instances of that prefab. It's more likely you have a code error that is accidentally changing all your instances. For example: How can I edit the components of an instantiated prefab? addresses editing the components of one prefab without changing any others.

You need to ensure you're referencing the specific object you're trying to change. Don't reference the prefab source, just the gameObject you want to change. If you change something that's common to all the instances, like a mesh or texture, it will affect all the instances, similar to this problem: How to make prefabs independent?

\$\endgroup\$
3
  • \$\begingroup\$ Thank you very much for the answer! May I ask you some additional questions?))) If I have correctly understood your answer, when prefab is on the scene, everything what I am doing with this instance of prefab (through its gameObject) is affecting only on this instance of prefab? \$\endgroup\$
    – bukka.wh
    Commented Dec 26, 2014 at 9:30
  • \$\begingroup\$ That's correct, changing an object in your scene will only change that object. That change will "break the connection" with your prefab. You do have the option of updating the prefab with those changes, but it's an explicit option you have to select. \$\endgroup\$
    – House
    Commented Dec 26, 2014 at 14:41
  • \$\begingroup\$ Thank you again for the answer!) Now I solved my problem. \$\endgroup\$
    – bukka.wh
    Commented Dec 27, 2014 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .