Search code examples
c#unity-game-engineprefab

How to update all unity prefabs from the main one through c#?


I have a script that updates the Sprite Renderer of the main prefab (the one that is in Assets) but this does not change or update the Sprite Renderer of the instances of the main Prefab. How can I change all instances of that prefab through c#?


Solution

  • If you try to made the changes at runtime will not work because the instances of the prefab are independant objects, if you want to change the sprite you should do it to each instance of the prefab.

    Prefabs are ready-made GameObjects, comparable to templates. A GameObject can be created once with all of its components and properties set up and stored as a prefab asset. This prefab can then be instantiated, which means an instance or copy of the prefab is spawned into the scene.

    If you want to replace that sprites on the editor you could do it directly on the prefab, or via a editor script that made the changes.