Hi guys,
am trying to disable the overlay thats on a button but its not working, her are some screen shoots Screenshot - 71f4139162270c8a05b84c33a6d253c3 - Gyazo and also this is what the game screen looks like Screenshot - 7d24fe060d3ed8141449ff71c37817f2 - Gyazo and now here is the code one which is the MainMenu, and the other which is the GameManager. thank you guys i hope you understand what am trying to do
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class MainMenu : MonoBehaviour
{
// shop menu
public GameObject shopButtonPrefab;
public GameObject shopButtonContainer;
public Material playerMaterial; // player skins
public Text currencyText;
// camre smorth movment
private const float CameraSpeed = 3.0f;
private Transform cameraTransform;
private Transform cameraDesiredLookAt;
// Use this for initialization
private void Start ()
{
cameraTransform = Camera.main.transform;
// getting the shop textures
ChangePlayerSkin(GameManager.Instance.currentSkinIndex);
currencyText.text = "" + GameManager.Instance.currency.ToString();
int textureIndex = 0;
Sprite[] textures = Resources.LoadAll<Sprite>("Player");
foreach(Sprite texture in textures)
{
GameObject container = Instantiate(shopButtonPrefab) as GameObject;
container.GetComponent<Image>().sprite = texture;
container.transform.SetParent(shopButtonContainer.transform, false);
// to have the button change the color
int index = textureIndex;
container.GetComponent<Button>().onClick.AddListener(() => ChangePlayerSkin(index));
if((GameManager.Instance.skinAvailability & 1 << index) == 1 << index)
{
container.transform.GetChild(0).gameObject.SetActive(false); // gets the price tag to be removed
}
textureIndex++;
}
}
// Update is called once per frame
private void Update ()
{
if(cameraDesiredLookAt != null)
{
// camera smoth rotatoin
cameraTransform.rotation = Quaternion.Slerp(cameraTransform.rotation, cameraDesiredLookAt.rotation, CameraSpeed * Time.deltaTime);
}
}
// camera rotatoin
public void LookAtMenu(Transform mainMenuTransform)
{
cameraDesiredLookAt = mainMenuTransform;
}
private void ChangePlayerSkin(int index)
{
if((GameManager.Instance.skinAvailability & 1 << index) == 1 << index)
{
// this cheks the index number is in the skin availibility
float x = (index % 4) * 0.25f;
float y = ((int)index / 4) * 0.25f;
if (y == 0.0f)
{
y = 0.75f;
}
else if (y == 0.25f)
{
y = 0.5f;
}
else if (y == 0.50f)
{
y = 0.25f;
}
else if (y == 0.75f)
{
y = 0;
}
playerMaterial.SetTextureOffset("_MainTex", new Vector2(x, y));
GameManager.Instance.currentSkinIndex = index;
GameManager.Instance.Save(); // this saves the game
}
else
{
// you dont have the have the skin do you want to buy it
int cost = 0; // skins going to be free
if(GameManager.Instance.currency >= cost)
{
GameManager.Instance.currency -= cost;
GameManager.Instance.skinAvailability += 1 << index;
GameManager.Instance.Save();
currencyText.text = " " + GameManager.Instance.currency.ToString();
shopButtonContainer.transform.GetChild(index).GetChild(0).gameObject.SetActive(false); // this is whats giving me the error
ChangePlayerSkin(index);
}
}
}
public void PlayGame()
{
SceneManager.LoadScene(1);
Time.timeScale = 1.0f;
}
}
the GameManager code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameManager : MonoBehaviour
{
private static GameManager instance;
public static GameManager Instance { get { return instance; } } // we can use this value anywhere in the game
public int currentSkinIndex = 0;
public int currency = 0;
public int skinAvailability = 1;
private void Awake()
{
instance = this;
DontDestroyOnLoad(gameObject); // this makes sure and doesnt destroy it when its new scene
if (PlayerPrefs.HasKey("CurrentSkin"))
{
// this checks if you have played before and it has eveything and it will laod it
currentSkinIndex = PlayerPrefs.GetInt("CurrentSkin");
currency = PlayerPrefs.GetInt("Currency");
skinAvailability = PlayerPrefs.GetInt("SkinAvailability");
}
else
{
Save();
}
}
public void Save()
{
// this is if you didnt play before so you create a new game
PlayerPrefs.SetInt("CurrentSkin", currentSkinIndex); // start off with the first skin
PlayerPrefs.SetInt("Currency", currency); // this will start you with no money
PlayerPrefs.SetInt("SkinAvailibility", skinAvailability);
}
}