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;
}
}
Check the android logs (with \tools\monitor.bat) for errors, an error can prevent some script from working.
Where is tools in unity ? sorry I'm kind of new to it :/ also thanks for replying :)
Sorry I found the tools my bad not in unity. This is what i'm getting any ideas ?
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.
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.
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
Follow this Question
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