Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Child Out of Bounds

Discussion in 'Scripting' started by Aymxne, Jun 16, 2016.

  1. Aymxne

    Aymxne

    Joined:
    Dec 26, 2015
    Posts:
    34
    Hi guys,

    am trying to disable the overlay thats on a button but its not working, her are some screen shoots https://gyazo.com/71f4139162270c8a05b84c33a6d253c3 and also this is what the game screen looks like https://gyazo.com/7d24fe060d3ed8141449ff71c37817f2 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 :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. public class MainMenu : MonoBehaviour
    7. {
    8.     // shop menu
    9.     public GameObject shopButtonPrefab;
    10.     public GameObject shopButtonContainer;
    11.     public Material playerMaterial; // player skins
    12.     public Text currencyText;
    13.  
    14.     // camre smorth movment
    15.     private const float CameraSpeed = 3.0f;
    16.     private Transform cameraTransform;
    17.     private Transform cameraDesiredLookAt;
    18.  
    19.     // Use this for initialization
    20.     private void Start ()
    21.     {
    22.         cameraTransform = Camera.main.transform;
    23.  
    24.         // getting the shop textures
    25.         ChangePlayerSkin(GameManager.Instance.currentSkinIndex);
    26.         currencyText.text = "" + GameManager.Instance.currency.ToString();
    27.  
    28.         int textureIndex = 0;
    29.         Sprite[] textures = Resources.LoadAll<Sprite>("Player");
    30.  
    31.         foreach(Sprite texture in textures)
    32.         {
    33.             GameObject container = Instantiate(shopButtonPrefab) as GameObject;
    34.             container.GetComponent<Image>().sprite = texture;
    35.             container.transform.SetParent(shopButtonContainer.transform, false);
    36.             // to have the button change the color
    37.             int index = textureIndex;
    38.             container.GetComponent<Button>().onClick.AddListener(() => ChangePlayerSkin(index));
    39.  
    40.             if((GameManager.Instance.skinAvailability & 1 << index) == 1 << index)
    41.             {
    42.                 container.transform.GetChild(0).gameObject.SetActive(false); // gets the price tag to be removed
    43.             }
    44.  
    45.             textureIndex++;
    46.         }
    47.  
    48.     }
    49.    
    50.     // Update is called once per frame
    51.     private void Update ()
    52.     {
    53.         if(cameraDesiredLookAt != null)
    54.         {
    55.             // camera smoth rotatoin
    56.             cameraTransform.rotation = Quaternion.Slerp(cameraTransform.rotation, cameraDesiredLookAt.rotation, CameraSpeed * Time.deltaTime);
    57.         }
    58.     }
    59.  
    60.     // camera rotatoin
    61.     public void LookAtMenu(Transform mainMenuTransform)
    62.     {
    63.         cameraDesiredLookAt = mainMenuTransform;
    64.     }
    65.  
    66.     private void ChangePlayerSkin(int index)
    67.     {
    68.  
    69.         if((GameManager.Instance.skinAvailability & 1 << index) == 1 << index)
    70.         {
    71.             // this cheks the index number is in the skin availibility
    72.  
    73.             float x = (index % 4) * 0.25f;
    74.             float y = ((int)index / 4) * 0.25f;
    75.  
    76.             if (y == 0.0f)
    77.             {
    78.                 y = 0.75f;
    79.             }
    80.             else if (y == 0.25f)
    81.             {
    82.                 y = 0.5f;
    83.             }
    84.             else if (y == 0.50f)
    85.             {
    86.                 y = 0.25f;
    87.             }
    88.             else if (y == 0.75f)
    89.             {
    90.                 y = 0;
    91.             }
    92.  
    93.             playerMaterial.SetTextureOffset("_MainTex", new Vector2(x, y));
    94.             GameManager.Instance.currentSkinIndex = index;
    95.             GameManager.Instance.Save(); // this saves the game
    96.  
    97.         }
    98.  
    99.         else
    100.         {
    101.             // you dont have the have the skin do you want to buy it
    102.             int cost = 0; // skins going to be free
    103.  
    104.             if(GameManager.Instance.currency >= cost)
    105.             {
    106.                 GameManager.Instance.currency -= cost;
    107.                 GameManager.Instance.skinAvailability += 1 << index;
    108.                 GameManager.Instance.Save();
    109.  
    110.                 currencyText.text = " " + GameManager.Instance.currency.ToString();
    111.                 shopButtonContainer.transform.GetChild(index).GetChild(0).gameObject.SetActive(false); // this is whats giving me the error
    112.                 ChangePlayerSkin(index);
    113.             }
    114.         }
    115.     }
    116.  
    117.     public void PlayGame()
    118.     {
    119.         SceneManager.LoadScene(1);
    120.         Time.timeScale = 1.0f;
    121.     }
    122. }
    123.  
    the GameManager code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     private static GameManager instance;
    8.     public static GameManager Instance { get { return instance; } } // we can use this value anywhere in the game
    9.  
    10.     public int currentSkinIndex = 0;
    11.     public int currency = 0;
    12.     public int skinAvailability = 1;
    13.  
    14.     private void Awake()
    15.     {
    16.  
    17.         instance = this;
    18.         DontDestroyOnLoad(gameObject); // this makes sure and doesnt destroy it when its new scene
    19.  
    20.         if (PlayerPrefs.HasKey("CurrentSkin"))
    21.         {
    22.             // this checks if you have played before and it has eveything and it will laod it
    23.             currentSkinIndex = PlayerPrefs.GetInt("CurrentSkin");
    24.             currency = PlayerPrefs.GetInt("Currency");
    25.             skinAvailability = PlayerPrefs.GetInt("SkinAvailability");
    26.  
    27.  
    28.         }
    29.         else
    30.         {
    31.             Save();
    32.         }
    33.     }
    34.  
    35.     public void Save()
    36.     {
    37.         // this is if you didnt play before so you create a new game
    38.         PlayerPrefs.SetInt("CurrentSkin", currentSkinIndex); // start off with the first skin
    39.         PlayerPrefs.SetInt("Currency", currency); // this will start you with no money
    40.         PlayerPrefs.SetInt("SkinAvailibility", skinAvailability);
    41.     }
    42.  
    43. }
    44.  
     
  2. Michael-N3rkmind

    Michael-N3rkmind

    Joined:
    Aug 16, 2015
    Posts:
    24
    How many sprites do you have under your resource folder?