Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script works in Unity but not on Androdi

Discussion in 'Scripting' started by hunsnowboarder, Aug 28, 2015.

  1. hunsnowboarder

    hunsnowboarder

    Joined:
    Aug 15, 2015
    Posts:
    3
    Hi Everyone!

    This is my very first post here and my very first and very very, primitively basic application written in Unity.
    So my application is a cookie fortune application which has two scenes. Clicking on the first scene will go to second scene and show a fortune sentence. The sentences are stored in an external txt file and a random function will output the random sentence.
    So in Unity this code works fine, but when ported to android instead of getting a random sentence I just see the default text (New text).
    This is my script for the second scene:

    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]public class RandomText : MonoBehaviour {
    4. [*]      
    5. [*]        public Text myText;
    6. [*]
    7.  
    8. [*]        void Start() {
    9. [*]
    10.  
    11. [*]                var myLines = File.ReadAllLines(@"..\FortuneCookie\Assets\quotes.txt");
    12. [*]                int lineCount = myLines.Length;
    13. [*]              
    14. [*]                int myRandomNumber = Random.Range (0,lineCount);
    15. [*]                string myQuote = myLines[myRandomNumber];
    16. [*]                              
    17. [*]
    18.  
    19. [*]                myText.text = myQuote;
    20. [*]        }
    21. [*]
    22.  
    23. [*]}
    24. [/LIST]
    25.  
    There are actually two issues with this script:
    1. When run in Unity the random function runs twice and I have an error but the script runs. The erro message is saying:
    "NullReferenceException: Object reference not set to an instance of an object
    RandomText.Start() (at Assets/RandomText.cs line: 14) Why is the code executed twice and why do I get that error message?

    2. In Unity it gives a random sentence, when ported to Android I just get the default input text (New text). What am I doing wrong? How can I solve this issue?

    Thank you for your help in advance!
     
    Last edited: Aug 28, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    [code ] [/code ] tags help with the readability when pasting in code into the forums

    error messages have line numbers, please include them
     
    hunsnowboarder likes this.
  3. hunsnowboarder

    hunsnowboarder

    Joined:
    Aug 15, 2015
    Posts:
    3
    Thank you! Corrected!
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Make sure the folders and file exist on the mobile device. The folder that you're referring to may not exist there after building the app.
    An alternative way to get this working may be the TextAsset class from the UnityAPI. I haven't used it yet, so i cannot tell you how well this will work for your purpose.
    And how do you know Random.Range runs twice? Maybe you've got two objects holding the script by accident?
     
    hunsnowboarder and Nigey like this.
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Yeah the folder directory will be inaccessible unless it's from the 'Resources' folder I think. Once the game's compiled into an .apk all the assets are compiled together, and their directory's are lost. Your best bet is something similar to this:

    Code (CSharp):
    1. // put those at the top
    2. using System.Text;
    3. using System.IO;
    4.  
    5. TextAsset textDeckPoints;
    6. textDeckPoints = Resources.Load ("nameoffile, excluding th e txt at the end") as TextAsset;
    7.  
    8. string[] lineDeckPoints = textDeckPoints.text.Split ('.');
    A bit messy, but you get the idea. There I'm just making string array with it, but you can do anything you like :). Just make sure you make a folder as: "Assets/Resources" and have anything you want to pull using the above in there.
     
    hunsnowboarder likes this.
  6. hunsnowboarder

    hunsnowboarder

    Joined:
    Aug 15, 2015
    Posts:
    3
    Wow man! Thanx a lot!!!
    Suddoha you guessed right, the code was running twice. It was added to the scene and also to the textbox on the scene.

    Nigey, thank you for making it clear what is the problem! Now it is clear that it was not able to find the txt. File! Thanks again guys for the help!
     
    Nigey likes this.