- Home /
UI Prefab not instantiating on build for Android.
I made a UI list instantiate for weapon upgrades. It works fine when running it in the editor but when I build it and try to run it in Android nothing pops up. While looking up what happens in logcat this error pops up:
I/Unity: NullReferenceException: Object reference not set to an instance of an object at CreateScrollList.PopulateList () [0x00000] in :0 at CreateScrollList.Start () [0x00000] in :0 (Filename: Line: -1)
What doesn't Make sense to me is that this if the object wasn't set to a reference wouldn't I get an error in the editor?
Here is what the scripts look like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class CreateScrollList : MonoBehaviour {
public GameObject upgradePanelPrefab;
public Transform contentPanelFist;
public Transform contentPanelSword;
public Transform contentPanelHammer;
public Transform contentPanelCannon;
public ShopGUICounter _ShopCounter;
public Text infoText;
private void Start(){
Debug.Log ("Marker 0");
PopulateList ();
}
private void PopulateList(){
Debug.Log ("Marker 1");
//Runs through all weapons in weapon info array
for(int i = 0; i < GameControl.control.WeaponInfo.Length; i++){
//Debug.Log (GameControl.control.WeaponInfo[i].weaponName);
//Runs through all weapon data looking for a match
for(int n = 0; n < GameControl.control.CurrentWeaponData.Length; n++){
//IF a match is found, a panel is created and given the correct level data
if (GameControl.control.WeaponInfo [i].ID == GameControl.control.CurrentWeaponData [n].WeaponID) {
GameObject newUpgradePanel = Instantiate (upgradePanelPrefab) as GameObject;
UpgradePanel upgradePanelScript = newUpgradePanel.GetComponent<UpgradePanel> ();
//Set Panel Info here
Debug.Log ("Marker 2");
upgradePanelScript.ItemName.text = GameControl.control.WeaponInfo [i].weaponName;
upgradePanelScript.ItemIcon.sprite = GameControl.control.WeaponInfo [i].weaponLogo;
if (GameControl.control.CurrentWeaponData [i].WeaponLevel == 0) {
upgradePanelScript.upgradeCost.text = GameControl.control.WeaponInfo [i].UpgradeCost [GameControl.control.CurrentWeaponData [n].WeaponLevel].ToString () + "\nUNLOCK";
if (GameControl.control.WeaponInfo [n].MinLevelRequired != 0) {
upgradePanelScript.upgradePanel.SetActive (false);
upgradePanelScript.reqPanelLevels.SetActive (true);
upgradePanelScript.reqTxtLevelAmt.text = "Level " + GameControl.control.WeaponInfo [n].MinLevelRequired;
}
if (GameControl.control.WeaponInfo [n].MinSkullsRequired != 0) {
upgradePanelScript.upgradePanel.SetActive (false);
upgradePanelScript.reqPanelSkulls.SetActive (true);
upgradePanelScript.reqTxtSkullAmt.text = "x " + GameControl.control.WeaponInfo [n].MinSkullsRequired;
}
} else {
upgradePanelScript.upgradeCost.text = GameControl.control.WeaponInfo [i].UpgradeCost [GameControl.control.CurrentWeaponData [n].WeaponLevel].ToString () + "\nUPGRADE";
}
//if players coin amount is < than current upgrade cost disable and enable req window
if(GameControl.control.CurrentPlayerData.coinAmt < GameControl.control.WeaponInfo [i].UpgradeCost[GameControl.control.CurrentWeaponData[n].WeaponLevel]){
upgradePanelScript.upgradeCost.color = new Color (131f/255.0f, 3f/255.0f, 3f/255.0f);
}
int indexWeapDataN = n;
int indexWeapInfoI = i;
upgradePanelScript.upgradeButton.onClick.AddListener( () => {UpgradeWeapon(indexWeapInfoI, indexWeapDataN, upgradePanelScript);});
upgradePanelScript.InfoButton.onClick.AddListener( () => {UpdateInfoPanel(indexWeapInfoI, indexWeapDataN);});
if (IsWeaponMaxed (indexWeapInfoI, indexWeapDataN)) {
PanelUpdateWeaponMaxed (upgradePanelScript);
}
//Places Panel In Correct Tab
switch (GameControl.control.WeaponInfo [i].type) {
case Weapon.Type.FIST:
newUpgradePanel.transform.SetParent (contentPanelFist);
break;
case Weapon.Type.SWORD:
newUpgradePanel.transform.SetParent (contentPanelSword);
break;
case Weapon.Type.HAMMER:
newUpgradePanel.transform.SetParent (contentPanelHammer);
break;
case Weapon.Type.CANNON:
newUpgradePanel.transform.SetParent (contentPanelCannon);
break;
default:
Debug.Log ("ERROR: No Match for weapon type");
break;
}
//Stops looking through weap data since weapon was found
newUpgradePanel.transform.localScale = new Vector3 (1f, 1f, 1f);
//Debug.Log ("populated");
break;
}
}
}
}
private void UpgradeWeapon(int indexWeapInfo, int indexWeapData, UpgradePanel CurrentPanel){
if (IsWeaponMaxed (indexWeapInfo, indexWeapData)) {
PanelUpdateWeaponMaxed (CurrentPanel);
} else {
if(GameControl.control.CurrentPlayerData.coinAmt < GameControl.control.WeaponInfo[indexWeapInfo].UpgradeCost[GameControl.control.CurrentWeaponData[indexWeapData].WeaponLevel]){
_ShopCounter.FlashInfoText ("You Cannot Afford Upgrade");
Debug.Log ("<b>ERROR:</b> Player Cannot Afford Upgrade.");
return;
}
GameControl.control.CurrentPlayerData.coinAmt -= GameControl.control.WeaponInfo [indexWeapInfo].UpgradeCost [GameControl.control.CurrentWeaponData [indexWeapData].WeaponLevel];
GameControl.control.CurrentWeaponData [indexWeapData].WeaponLevel += 1;
CurrentPanel.upgradeCost.text = GameControl.control.WeaponInfo [indexWeapInfo].UpgradeCost [GameControl.control.CurrentWeaponData [indexWeapData].WeaponLevel].ToString () + "\nUPGRADE";
_ShopCounter.UpdateCounterTextPlayerData ();
//if players coin amount is < than current upgrade cost disable and enable req window
if (GameControl.control.CurrentPlayerData.coinAmt < GameControl.control.WeaponInfo [indexWeapInfo].UpgradeCost [GameControl.control.CurrentWeaponData [indexWeapData].WeaponLevel]) {
CurrentPanel.upgradeCost.color = new Color (131f / 255.0f, 3f / 255.0f, 3f / 255.0f);
}
if(IsWeaponMaxed(indexWeapInfo, indexWeapData)){
PanelUpdateWeaponMaxed(CurrentPanel);
}
}
UpdateInfoPanel (indexWeapInfo, indexWeapData);
}
private bool IsWeaponMaxed(int indexInfo, int indexData){
//Debug.Log (GameControl.control.WeaponInfo [indexInfo].MaxLevelCap + " " + GameControl.control.CurrentWeaponData [indexData].WeaponLevel);
if (GameControl.control.WeaponInfo [indexInfo].MaxLevelCap == GameControl.control.CurrentWeaponData [indexData].WeaponLevel) {
return true;
} else {
return false;
}
}
private void PanelUpdateWeaponMaxed(UpgradePanel panel){
panel.upgradePanel.SetActive (false);
panel.maxedOutPanel.SetActive (true);
}
private void UpdateInfoPanel(int indexWeapInfo, int indexWeapData){
if(GameControl.control.CurrentWeaponData[indexWeapData].WeaponLevel == 0){
infoText.text =
"<size=18>" + GameControl.control.WeaponInfo [indexWeapInfo].weaponName + "</size>"
+ "\nLEVEL: WEAPON IS LOCKED ";
} else {
infoText.text =
"<size=18>" + GameControl.control.WeaponInfo [indexWeapInfo].weaponName + "</size>"
+ "\nLEVEL: " + GameControl.control.CurrentWeaponData [indexWeapData].WeaponLevel
+ "\nDamage: " + GameControl.control.WeaponInfo[indexWeapInfo].weaponDamageNormal[GameControl.control.CurrentWeaponData[indexWeapData].WeaponLevel] + " <color=green> +" + ((GameControl.control.WeaponInfo[indexWeapInfo].weaponDamageNormal[GameControl.control.CurrentWeaponData[indexWeapData].WeaponLevel] + 1) - GameControl.control.WeaponInfo[indexWeapInfo].weaponDamageNormal[GameControl.control.CurrentWeaponData[indexWeapData].WeaponLevel]) + "</color>";
}
}
}
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
public class UpgradePanel : MonoBehaviour {
public Button InfoButton;
public Button upgradeButton;
public GameObject upgradePanel;
public Text upgradeCost;
public GameObject maxedOutPanel;
public GameObject reqPanelLevels;
public GameObject reqPanelSkulls;
public Text reqTxtLevelAmt;
public Text reqTxtSkullAmt;
public Text ItemName;
public Image ItemIcon;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ShopGUICounter : MonoBehaviour {
public Text coinAmt;
public Text seedAmt;
public Text skullAmt;
public Text InfoText;
private float currentTime = 0.0f;
private float txtFadeTime = 3.0f;
private float txtHoldTime = 1.0f;
private bool isInfoTxtVisable = false;
void Start () {
UpdateCounterTextPlayerData ();
UpdateCounterTextLevelData ();
InfoText.color = new Color (InfoText.color.r, InfoText.color.b, InfoText.color.b, 0f);
}
public void UpdateCounterTextLevelData(){
int skullTotal = 0;
for (int i = 0; i > GameControl.control.CurrentLevelData.Length; i++) {
if (GameControl.control.CurrentLevelData [i].skullAFound) {
skullTotal += 1;
}
if(GameControl.control.CurrentLevelData[i].skullBFound){
skullTotal += 1;
}
if(GameControl.control.CurrentLevelData[i].skullCFound){
skullTotal += 1;
}
}
skullAmt.text = skullTotal.ToString();
}
public void UpdateCounterTextPlayerData(){
coinAmt.text = GameControl.control.CurrentPlayerData.coinAmt.ToString ();
seedAmt.text = GameControl.control.CurrentPlayerData.seedAmt.ToString ();
}
public void FlashInfoText(string newTxt){
currentTime = 0.0f;
if (!isInfoTxtVisable) {
isInfoTxtVisable = true;
InfoText.text = newTxt;
StartCoroutine (FadeOutText ());
}
}
private IEnumerator FadeOutText(){
while (currentTime < txtHoldTime + txtFadeTime) {
if (currentTime > txtHoldTime) {
float alpha = Mathf.Lerp (1f, 0f, (currentTime - txtHoldTime)/txtHoldTime);
InfoText.color = new Color (InfoText.color.r, InfoText.color.b, InfoText.color.b, alpha);
} else if(currentTime == 0.0f){
InfoText.color = new Color (InfoText.color.r, InfoText.color.b, InfoText.color.b, 1f);
}
currentTime += Time.deltaTime;
yield return null;
}
InfoText.color = new Color (InfoText.color.r, InfoText.color.b, InfoText.color.b, 0f);
isInfoTxtVisable = false;
yield break;
}
}
This is how it looks in the editor:
This is show it looks in android build:
Any ideas? I'm really not sure what I'm doing wrong here.....
Answer by phxmichael · Feb 08, 2017 at 09:07 AM
After alot of debugging I narrowed the problem to my weaponInfo class. I had turned my weaponInfo class into an asset that inherited from mono behavior. Once I deleted this and created my WeaponInfo class through constructors. Everything worked like normal when I built it. Not sure the specifics of why this worked so if someone can elaborate that would be greatly appreciated.
Your answer
Follow this Question
Related Questions
Clamp To The Screen Edge 0 Answers
Store/Stack Items 0 Answers
UIText not updating on Android build 0 Answers
Unity 2d mobile android GUI texture button 1 Answer
Choppy Character Movement Unity 2D 0 Answers