• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by phxmichael · Jan 29, 2017 at 09:36 AM · c#android2dgui

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 not$$anonymous$$ng pops up. W$$anonymous$$le looking up what happens in logcat t$$anonymous$$s 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 t$$anonymous$$s 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(){
 
         w$$anonymous$$le (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;
 
 
     }
 }
 

T$$anonymous$$s is how it looks in the editor: alt text

T$$anonymous$$s is show it looks in android build: alt text

Any ideas? I'm really not sure what I'm doing wrong here.....

game-build.png (425.8 kB)
editor-build.png (110.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 t$$anonymous$$s and created my WeaponInfo class through constructors. Everyt$$anonymous$$ng worked like normal when I built it. Not sure the specifics of why t$$anonymous$$s worked so if someone can elaborate that would be greatly appreciated.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Clamp To The Screen Edge 0 Answers

Store/Stack Items 0 Answers

UIText not updating on Android build 0 Answers

2D multi touch 1 Answer

Unity 2d mobile android GUI texture button 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges