Changing Prefab setting through scripting (C#)

Is it possible to use a script that would change a value (for instance a color of a material on a prefab) through scripting? I don’t mean an instance of a prefab in the scene, but the very prefab itself, so that it would be affected across all scenes?

The reason why I ask is that I want to give the player a choice of choosing between 2 sides - each using their own sprites (2D). I’ve came up with 3 solutions.

1 - Confirming the side for Instantiating everywhere.

Basically every time the script would need to instantiate something playerwise or enemywise, it’d do an if statement (since the playerSide variable would be static) – so if(playerSide == 1) put one side; else(playerSide == 2) put other side; (I’m afraid this could effect the performance drastically - since I’m not sure yet how many if statements would be applied).

2 - Putting the GameObject’s in an array

Again, same stuff but only withough if statements, but just with an array of GameObjects and simply put in Instantiate(GameObject[playerChoice], location, rotation); – but again, I’m afraid it might cost a lot because of so many refferences (since I’d pass the 2 sides on every script).

3 - My question…

The idea behind this is that it’d slow down the game hard once, during it’s “first load”, when the player chooses a side, but then run normal, since the sprites are where they should be.


Sorry for the long question. I listed my other ways above because if changing the prefab is impossible, I’d be very greatfull if someone could tell me which of the above solutions might suit me best? Or if there’s something else I should consider doing…?

Thank you.

You can change a value of a prefab through scripting, as long as you can access the prefab.

For example, let’s say you have a GameObject in you scene that will function as your PrefabManager. You’ll need to create a PrefabManager script and attach it on an empty GameObject, also named PrefabManager. This script will have a public GameObject array to hold all your prefabs. In this way, you’ll just drag-n-drop all the prefabs you want, from the Assets folder to the “Prefabs” field in the inspector. Then, all these prefabs will be available inside your script. You could also have a Materials array in your PrefabManager script (or anything else you like). Here’s an example of how you would change your prefab materials:

using UnityEngine;

//Get a reference to this script from the script that gets input from the user
//This way you can call the SetMaterial method.
//Alternatively, include this functionality in the script that gets the input and instantiates the prefab.

public class PrefabManager : MonoBehaviour {

    public GameObject[] Prefabs;
    public Material[] Materials;


    //// Use this for initialization
    //void Start () {
	
    //}
	
    //// Update is called once per frame
    //void Update () {
	
    //}

    //call this method with any prefab index and any material index
    void SetMaterial(int prefabIndex, int materialIndex)
    {
        Prefabs[prefabIndex].renderer.material = Materials[materialIndex];
    }
}

Of course this is just an example to demonstrate the way to change a prefab through scripting. You can customize this to suit your specific case.

Just make two prefab