Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by MrMallon · Jan 14, 2016 at 10:12 PM · c#androidtextnot workingupdating

text not changing in build for android unity

The text updates fine in the editor and on a pc build but doesn't work on android builds for some reason any ideas?

     using UnityEngine;
     using System.Text;
     using System.IO;
     using System.Collections;
     using System;
     using UnityEngine.UI;
     using System.Collections.Generic;
     
     public class LoadSave : MonoBehaviour
     {
        // private string inventoryFilePath;
     
         private StreamWriter writer;
         public Text text;
         public Text hutTextField;
         void Start()
         {
            loadData();
             //loadTextfields();
         }
     
         void Update()
         {
             var inventoryComponent = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
             var items = inventoryComponent.inventory;
             hutTextField = GameObject.Find("mytext").GetComponent<Text>();
             items[1].itemQuantity += 1;
             string test = items[1].itemQuantity.ToString();
             hutTextField.text = test;
             
     
     
         }   
      private void loadData()
         {
             StreamReader read = new StreamReader("inventory.txt");
             var inventoryComponent = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
             var items = inventoryComponent.inventory;
             string readfile = read.ReadLine();
     
             while(readfile != null)
             {
                 char[] delimiter = {';'};
                 string[] input = readfile.Split(delimiter);
                 
                 items.Add(new Item(input[0], int.Parse(input[1]), input[2], int.Parse(input[3]), int.Parse(input[4]), int.Parse(input[5]), input[6], input[7], input[8]));
                 readfile = read.ReadLine();
             }
             read.Close();
         }
     
         public void saveData()
         {
            // Debug.Log("HERE");
             var itemDb =  GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
             writer = new StreamWriter("inventory.txt");
             var items = itemDb.items;
     
             for(int i = 0; i < items.Count; i++)
             {
                 string output = items[i].itemName + ";" + items[i].itemID.ToString() + ";" 
                     + items[i].itemDesc + ";" + items[i].itemQuantity.ToString() + ";" 
                     + items[i].itemPower.ToString() + ";" + items[i].itemWeight.ToString() + ";" 
                     + items[i].itemRequirement1 + ";" + items[i].itemRequirement2+ ";" + items[i].itemType;
                 writer.WriteLine(output);
             }
              writer.Close();
     
         }
     }

INVETORY CLASS

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
     public List<Item> inventory = new List<Item>();
 
 
     // Use this for initialization
     void Start () {
     }
 
 
     // Update is called once per frame
     void Update()
     {
 
     }
 }

ITEMDATABASE CLASS

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ItemDatabase : MonoBehaviour {
     public List<Item> items = new List<Item>();
 }

ITEMS CLASS

 using UnityEngine;
 using System.Collections;
 using System;
 
 [System.Serializable]
 public class Item {
 
     public string itemName;
     public int itemID;
     public string itemDesc;
     public Texture2D itemIcon;
     public int itemQuantity;
     public int itemPower;
     public int itemWeight;
     public string itemRequirement1;
     public string itemRequirement2;
     public Itemtype itemType;
     
     public enum Itemtype
     {
         Weapon,
         Consumable,
         normal
     }
 
     public Item(string name, int ID, string desc, int quantity, int power, int weight, string requirement1, string requirement2, string type)
     {
         var convertType = (Itemtype)Enum.Parse(typeof(Itemtype), type);
 
         itemName = name;
         itemID = ID;
         itemDesc = desc;
         itemIcon = Resources.Load<Texture2D>("Item Icons/" + name);
         itemQuantity = quantity;
         itemPower = power;
         itemWeight = weight;
         itemRequirement1 = requirement1;
         itemRequirement2 = requirement2;
         itemType = convertType;
     }
 }
 


Comment
Add comment · Show 6
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
avatar image lassade · Jan 15, 2016 at 02:33 AM 1
Share

Check the android logs (with \tools\monitor.bat) for errors, an error can prevent some script from working.

avatar image MrMallon lassade · Jan 15, 2016 at 11:53 AM 0
Share

Where is tools in unity ? sorry I'm kind of new to it :/ also thanks for replying :)

avatar image MrMallon lassade · Jan 15, 2016 at 12:17 PM 0
Share

Sorry I found the tools my bad not in unity. This is what i'm getting any ideas ? alt text

capture.png (14.6 kB)
avatar image corn MrMallon · Jan 15, 2016 at 01:05 PM 0
Share

ArgumentOutOfRangeException means that you are trying to access an index which has no entry in your array. This means that your items array has less than 2 elements (0 or 1), so items[1] does not exist.

Always remember to check if your index is valid :

 if (items.Length >= 2) 
 {
      items[1].itemQuantity += 1;
      text.text = items[1].itemQuantity.ToString();
 }

That way you won't get exceptions or errors.

So the problem here is that items[1] doesn't exist. Please post the rest of your code (LoadSave and Inventory) so we can find what's wrong.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by corn · Jan 17, 2016 at 03:34 PM

Alright, the problem is right there :

 StreamReader read = new StreamReader("inventory.txt");

This won't work on Android, because after building and packaging your app, your inventory.txt file will definitely not be where you expect it to be in the hierarchy. So loadData will fail and items will be empty, hence the ArgumentOutOfRangeException.

If you really want to use .txt files, you need to use Application.persistentDataPath, and save/load your file there. However, using PlayerPrefs would be far more convenient for storing a small set of values.

Comment
Add comment · Show 1 · 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
avatar image MrMallon · Jan 17, 2016 at 11:08 PM 0
Share

Thanks that worked but is the file only temporarily stored there? As in is it destroyed if the app is closed ? Is playerPrefs easily set up?

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.

Follow this Question

Answers Answers and Comments

67 People are following this question.

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

Related Questions

Buttons is not working correct on android 0 Answers

Damage Text pop-up when enemy is hit 1 Answer

How To Load TTF Font From External File 0 Answers

My method is being repeated several times during a single input. 1 Answer

How to link up instantiated text in list to allow buttons to adjust number shown, c# 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