Search code examples
unity-game-enginegame-development

Prefab loading the wrong colors (materials)


I am using the runner template in Unity Hub, and I have created my own collectibles in the game. However, some of them change the material after pressing play, and I am having trouble identifying the problem. This issue only affects certain prefabs, while others do not have this problem.

Please see the following images for reference:

  • Before pressing play:

collectable fries that are partially red and partially yellow

  • After pressing play:

collectable fries that are entirely yellow


Solution

  • The code for creating the levels in the runner game also sets their material colors. In my opinion this feels quite unnecessary, considering that all the prefabs are already the colors they are supposed to be. To get rid of this behaviour, comment out or remove line 180 of the GameManager.cs script.

    // Set Base Color
    Spawnable spawnable = go.GetComponent<Spawnable>();
    if (spawnable != null)
    {
        // spawnable.SetBaseColor(spawnableObject.BaseColor); // <= comment out this line
        spawnable.SetScale(scale);
        levelManager.AddSpawnable(spawnable);
    }
    

    If this functionality is needed for anything, you're probably going to have to reimplement it in a way that doesn't recolor your custom objects.