title: Replace material to material
body: <p>How to add sprite to player material with script. I have a shop menu of player and when a like to select one sprite i like to add to player material but I don't know how to do this. I have my code but just this I don't know how to add. This is my code I made like this but don't work can someone tell me how to add playerSprite to playermaterial.</p>
  
  <pre class="lang-c# prettyprint-override"><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class ShopMenuNew : MonoBehaviour
  {
      public Transform spritePanel;
  
      public Text spriteBuySetText;
      public Text goldText;
  
      [SerializeField]
      private int[] spriteCost;
      private int selectedSpriteIndex;
      private int activeSpriteIndex;
  
  
  
      // Use this for initialization
      void Start ()
      {
  
          GameManager.Instance.state.gold = 999;
  
          UpdateGoldText();
  
          InitShop();
  
          OnSpriteSelect(GameManager.Instance.state.activeSprite);
          SetSprite(GameManager.Instance.state.activeSprite);
      }
  
      // Update is called once per frame
      void Update () {
  
      }
  
      private void InitShop()
      {
          if (spritePanel == null)
              Debug.Log("You did not asign the sprite panel in the inspector");
  
          int i = 0;
          foreach (Transform t in spritePanel)
          {
              int currentIndex = i;
  
              Button b = t.GetComponent&lt;Button&gt;();
              b.onClick.AddListener(() =&gt; OnSpriteSelect(currentIndex));
  
              Image img = t.GetComponent&lt;Image&gt;();
              img.sprite = GameManager.Instance.IsSpriteOwned(i) ? GameManager.Instance.playerSprite[currentIndex] : GameManager.Instance.playerSpriteBuy[currentIndex] ;
  
              i++;
          }
  
      }
  
      private void SetSprite(int index)
      { 
          activeSpriteIndex = index;
          GameManager.Instance.state.activeSprite = index;
          GameManager.Instance.playerMaterial = GameManager.Instance.playerSprite[index];
  
          spriteBuySetText.text = "Current";
          GameManager.Instance.Save();
      }
  
  
      private void UpdateGoldText()
      {
          goldText.text = GameManager.Instance.state.gold.ToString();
      }
  
      private void OnSpriteSelect(int currentIndex)
      {
          Debug.Log("Selecting color button :" + currentIndex);
  
          if (selectedSpriteIndex == currentIndex)
              return;
  
  
  
          selectedSpriteIndex = currentIndex;
  
          if (GameManager.Instance.IsSpriteOwned(currentIndex))
          {
              if (activeSpriteIndex == currentIndex)
              {
                  spriteBuySetText.text = "Current";
              }
              else
              {
                  spriteBuySetText.text = "Select";
              }
  
          }
          else
          {
              spriteBuySetText.text = "Buy: " + spriteCost[currentIndex].ToString();
          }
      }
  
      public void OnSpriteBuySet()
      {
          Debug.Log("Buy Sprite");
  
          if (GameManager.Instance.IsSpriteOwned(selectedSpriteIndex))
          {
              SetSprite(selectedSpriteIndex);
              GameManager.Instance.Save();
          }
          else
          {
              if(GameManager.Instance.BuySprite(selectedSpriteIndex,spriteCost[selectedSpriteIndex]))
              {
                  SetSprite(selectedSpriteIndex);
                  spritePanel.GetChild(selectedSpriteIndex).GetComponent&lt;Image&gt;().sprite = GameManager.Instance.playerSprite[selectedSpriteIndex];
  
                  UpdateGoldText();
              }
              else
              {
                  Debug.Log("Not enough Gold");
              }
          }
      }
  }
  </code></pre>
  
  <pre class="lang-c# prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class GameManager : MonoBehaviour
  {
  
      public static GameManager Instance { set; get; }
      public SaveState state;
  
      public int currentSkinIndex = 0;
      public int currency = 0;
      public int skinAvilability = 1;
      public int coins;
  
      public Material playerMaterial;
      public Sprite[] playerSprite;
      public Sprite[] playerSpriteBuy;
      public Texture[] textures;
  
  
      private void Awake ()
      {
          DontDestroyOnLoad(gameObject);
  
          Instance = this;
          Load();
      }
  
      public void Save()
      {
          PlayerPrefs.SetString("save",Helper.Serialize&lt;SaveState&gt;(state));
      }
  
      public void Load()
      {
          if (PlayerPrefs.HasKey("save"))
          {
              state = Helper.Deserialize&lt;SaveState&gt;(PlayerPrefs.GetString("save"));
          }
          else
          {
              state = new SaveState();
              Save();
              Debug.Log("No Save file found, creating a new one!");
          }
      }
  
      public bool IsSpriteOwned(int index)
      {
          return (state.spriteOwned &amp; (1 &lt;&lt; index)) != 0;
      }
  
      public void UnlockSprite(int index)
      {
          state.spriteOwned |= 1 &lt;&lt; index;
      }
  
      public bool BuySprite(int index, int cost)
      {
          if(state.gold &gt;= cost)
          {
              state.gold -= cost;
              UnlockSprite(index);
  
              Save();
  
              return true;
          }
          else
          {
              return false;
          }
      }
  
      public void ResetSave()
      {
          PlayerPrefs.DeleteKey("save");
      }
  
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/GCXxB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GCXxB.png" alt="Screenshot of game"></a></p>