Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Switching sprites on Object only works in Editor, not during build.

Discussion in 'Scripting' started by mrCharli3, May 3, 2018.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    418
    I've made a Character creation screen for my 2d game, the sprites used in the character creation (hair, shirt, pants, etc) are stored in a Scriptable Object. The Atlas containing those Sprites is in my Resources folder.

    Once The player has finished creating his character, I save all new sprites and their color in my player data, like so:

    Code (CSharp):
    1.  
    2.         DataManager.instance.playerData.loadOut.hair = characterSetup.hair; //hair is a Sprite
    3.         DataManager.instance.playerData.loadOut.eyebrowShape = characterSetup.eyebrowShape;
    4.         DataManager.instance.playerData.loadOut.eyeShape = characterSetup.eyeShape;
    5.         DataManager.instance.playerData.loadOut.facialHair = characterSetup.facialHair;
    6.         //...
    7.  
    I then Instantiate my Player in my world, and use these values to set his appearance, like so:

    Code (CSharp):
    1.  
    2.         _character.hair = DataManager.instance.playerData.loadOut.hair;
    3.         _character.hairColor = DataManager.instance.playerData.loadOut.hairColor;
    4.         _character.facialHair = DataManager.instance.playerData.loadOut.facialHair;
    5.         //...
    6.  
    The problem is, this only works in editor. If I build the game and play, the only change that has any effect on the Player that gets Instantiated in my world is the skin color (where I do not reference any sprite), otherwise he has his default appearance.
    It is clear that somehow my management of Sprites only work in Editor and not in a build, but what am I doing that is causing this?

    Hair Sprites stored in an SO, loaded during character creation and used to set new appearance:



    What my PlayerData looks like in the inspector, this is what is used to set the player appearance:



    [SOLVED]
     
    Last edited: May 3, 2018
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,283
    Files in the Resources folder need to be loaded manually at runtime. Either write the code to do that, or just move the file out of Resources.

    Note, the official best practices about the Resources folder recommend:

     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    418
    Thanks. I'll try that!
     
  4. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    88
    Is this a Mobile Game or PC Game.