Activate and Buy

Hi a I wrote some scripts to create a sort of shop but they didn’t work. I would that the hats and then when I buy them,in another menu they are activated by the purchase.In a second time aI would switch off from hat to hat
Someone could help ?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Buyer : MonoBehaviour
{
    public int price = 0;
    private Text coinsText;
    public int coinCount = 0;
    private CoinCounter coinCounter;
    void Start ()
    {
        coinCounter = GameObject.Find("CoinCount").GetComponent<CoinCounter>();
        if(!PlayerPrefs.HasKey("coins")) // if the key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("coins", coinCounter.coinCount); // create it and set it to the value of coinCounter.coinCount
        }
        coinCounter.coinCount = PlayerPrefs.GetInt("coins"); // get the PlayerPrefs coins amount
        // check to see if the PlayerPrefs file has stored any hats
        if(!PlayerPregs.HasKey("Cappello1")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello1", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPregs.HasKey("Cappello2")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello2", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPregs.HasKey("Cappello3")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello3", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPregs.HasKey("Cappello4")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello4", 0); // create the key and set it's value to 0 (false)
        }
        PlayerPrefs.Save(); // save the changes to the PlayerPrefs file
    }
  
    public void Buy()
    {
        if (coinCounter.coinCount < price) // if the user doesn't have enough coins
        {
            return; // don't do anything.
        }
        if (coinCounter.coinCount >= price) // if the user has enough coins
        {
            if(this.name == "Hat1" && PlayerPrefs.GetInt("Cappello1") == 0) // if this is the Hat1 button & the player doesn't already own Hat #1
            {
                coinCounter.coinCount -= price; // take away the cost of this item
                PlayerPrefs.SetInt("Cappello1", 1); // make it so the player has access to Hat #1
            }
            if(this.name == "Hat2" && PlayerPrefs.GetInt("Cappello2") == 0) // if this is the Hat2 button & the player doesn't already own Hat #2
            {
                coinCounter.coinCount -= price; // take away the cost of this item
                PlayerPrefs.SetInt("Cappello2", 1); // make it so the player has access to Hat #2
            }
            if(this.name == "Hat3" && PlayerPrefs.GetInt("Cappello3") == 0) // if this is the Hat3 button & the player doesn't already own Hat #3
            {
                coinCounter.coinCount -= price; // take away the cost of this item
                PlayerPrefs.SetInt("Cappello3", 1); // make it so the player has access to Hat #3
            }
            if(this.name == "Hat4" && PlayerPrefs.GetInt("Cappello4") == 0) // if this is the Hat4 button & the player doesn't already own Hat #4
            {
                coinCounter.coinCount -= price; // take away the cost of this item
                PlayerPrefs.SetInt("Cappello4", 1); // make it so the player has access to Hat #4
            }
            coinsText.text = coinCounter.coinCount.ToString(); // change the coinsText to be the user's current coin amount
            PlayerPrefs.SetInt("coins", coinCounter.coinCount); // put the current coin amount in the PlayerPrefs file
            PlayerPrefs.Save(); // and finally, save the PlayerPrefs file.
        }
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChooseHat : MonoBehaviour {
  
    public GameObject hat1;
    public GameObject hat2;
    public GameObject hat3;
    public GameObject hat4;
  
    void Start()
    {
        // check to see if the PlayerPrefs file has stored any hats
        if(!PlayerPregs.HasKey("Cappello1")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello1", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPregs.HasKey("Cappello2")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello2", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPregs.HasKey("Cappello3")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello3", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPregs.HasKey("Cappello4")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello4", 0); // create the key and set it's value to 0 (false)
        }
        PlayerPrefs.Save(); // save the changes to the PlayerPrefs file
    }
  
    public void Hat1 () {
        hat1 = PlayerPrefs.GetInt("Cappelo1"); // check the value stored in the PlayerPrefs file
        if(hat1 == 1) // if the player owns hat 1
        {
            hat1.SetActive(true); // it can be active
            hat2.SetActive(false);
            hat3.SetActive(false);
            hat4.SetActive(false);
        }
        if(hat1 == 0)  // if the player does not own hat 1
        {
            hat1.SetActive(false); // make it inactive
            hat2.SetActive(false);
            hat3.SetActive(false);
            hat4.SetActive(false);
        }
    }
  
    public void Hat2 () {
        hat2 = PlayerPrefs.GetInt("Cappelo2"); // check the value stored in the PlayerPrefs file
        if(hat2 == 1) // if the player owns hat 2
        {
            hat1.SetActive(false); // it can be active
            hat2.SetActive(true);
            hat3.SetActive(false);
            hat4.SetActive(false);
        }
        if(hat2 == 0)  // if the player does not own hat 2
        {
            hat1.SetActive(false); // make it inactive
            hat2.SetActive(false);
            hat3.SetActive(false);
            hat4.SetActive(false);
        }
    }
  
    public void Hat3 () {
        hat3 = PlayerPrefs.GetInt("Cappelo3"); // check the value stored in the PlayerPrefs file
        if(hat3 == 1) // if the player owns hat 3
        {
            hat1.SetActive(false); // it can be active
            hat2.SetActive(false);
            hat3.SetActive(true);
            hat4.SetActive(false);
        }
        if(hat3 == 0) // if the player does not own hat 3
        {
            hat1.SetActive(false); // make it inactive
            hat2.SetActive(false);
            hat3.SetActive(false);
            hat4.SetActive(false);
        }
    }   
  
    public void Hat4 () {
        hat4 = PlayerPrefs.GetInt("Cappelo4"); // check the value stored in the PlayerPrefs file
        if(hat4 == 1) // if the player owns hat 4
        {
            hat1.SetActive(false); // it can be active
            hat2.SetActive(false);
            hat3.SetActive(false);
            hat4.SetActive(true);
        }
        if(hat4 == 0) // if the player does not own hat 4
        {
            hat1.SetActive(false); // make it inactive
            hat2.SetActive(false);
            hat3.SetActive(false);
            hat4.SetActive(false);
        }
    }
  
}

You’ll need to be more specific about what the problem is than just saying they “didn’t work.”

Assets/EndlessJumper/Scenes/ChooseHat.cs(34,17): error CS0029: Cannot implicitly convert type `int' to `UnityEngine.GameObject'
Assets/EndlessJumper/Scenes/ChooseHat.cs(34,17): error CS0029: Cannot implicitly convert type `int' to `UnityEngine.GameObject'

You’re trying to use the ‘hat1’ variable as both ‘the hat object itself’ (which is a GameObject) and ‘what is hat1 set to in PlayerPrefs’ (which is a number). You can’t use the same variable for both things; use a separate variable for storing the value returned by PlayerPrefs.GetInt().

ok I’ve resolved in this way

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChooseHat : MonoBehaviour {

    public GameObject Hat1;
    public GameObject Hat2;
    public GameObject Hat3;
    public GameObject Hat4;
    public int hat1;
    public int hat2;
    public int hat3;
    public int hat4;
    void Start()
    {
        // check to see if the PlayerPrefs file has stored any hats
        if(!PlayerPrefs.HasKey("Cappello1")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello1", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPrefs.HasKey("Cappello2")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello2", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPrefs.HasKey("Cappello3")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello3", 0); // create the key and set it's value to 0 (false)
        }
        if(!PlayerPrefs.HasKey("Cappello4")) // if this key does not exist in the PlayerPrefs file
        {
            PlayerPrefs.SetInt("Cappello4", 0); // create the key and set it's value to 0 (false)
        }
        PlayerPrefs.Save(); // save the changes to the PlayerPrefs file
    }
   
    public void Capello1 () {
        hat1 = PlayerPrefs.GetInt("Cappello1"); // check the value stored in the PlayerPrefs file
        if(hat1 == 1) // if the player owns hat 1
        {
            Hat1.SetActive(true); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(false);
            Hat4.SetActive(false);
        }
        if(hat1 == 0)  // if the player does not own hat 1
        {
            Hat1.SetActive(false); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(false);
            Hat4.SetActive(false);
        }
    }
   
    public void Cappello2 () {
        hat2 = PlayerPrefs.GetInt("Cappello2"); // check the value stored in the PlayerPrefs file
        if(hat2 == 1) // if the player owns hat 2
        {
            Hat1.SetActive(true); // it can be active
            Hat2.SetActive(true);
            Hat3.SetActive(false);
            Hat4.SetActive(false);
        }
        if(hat2 == 0)  // if the player does not own hat 2
        {
            Hat1.SetActive(false); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(false);
            Hat4.SetActive(false);
        }
    }
   
    public void Cappello3 () {
        hat3 = PlayerPrefs.GetInt("Cappello3"); // check the value stored in the PlayerPrefs file
        if(hat3 == 1) // if the player owns hat 3
        {
            Hat1.SetActive(false); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(true);
            Hat4.SetActive(false);
        }
        if(hat3 == 0) // if the player does not own hat 3
        {
            Hat1.SetActive(false); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(false);
            Hat4.SetActive(false);
        }
    }   
   
    public void Cappello4() {
        hat4 = PlayerPrefs.GetInt("Cappello4"); // check the value stored in the PlayerPrefs file
        if(hat4 == 1) // if the player owns hat 4
        {
            Hat1.SetActive(false); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(false);
            Hat4.SetActive(true);
        }
        if(hat4 == 0) // if the player does not own hat 4
        {
            Hat1.SetActive(false); // it can be active
            Hat2.SetActive(false);
            Hat3.SetActive(false);
            Hat4.SetActive(false);
        }
    }
   
}

Now I’ve atatched buyer to evenry single button while choose hat to the camera .Every button has the function buy but I’m not able to buy I’ve checked all seems good
NullReferenceException: Object reference not set to an instance of an object
Buyer.Buy () (at Assets/EndlessJumper/Scripts/Buyer.cs:71)