Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED!] Help Needed With Android Scripting

Discussion in 'Scripting' started by Chris-Ramer, Mar 21, 2016.

  1. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    I am ready to release a demo of my project to my Android device. The problem is that when I set the platform to Android in the Unity build settings menu, I get thousands of compiler errors that I never got before.

    I've read in the manual under "Getting Started with Android Development" that "These errors are usually easy to fix if you make sure all variables are explicitly typed or use type inference on initialization." I don't know what those mean since I am new to scripting and this is my first attempt to build to Android.

    When I was building to WebGL earlier today, I learned that I needed to put all the scripts under a folder called "Editor" in which I did and things worked fine from there, but if I have the scripts under the Editor folder when building to Android, sure, I don't get any compiler errors, but none of the scripts work. When I attempt to drop a script onto a game object, I get a message that reads: "The script is an editor script." And when I remove them from the Editor folder and have them in another folder of not in a folder at all and I try to drop them back onto an object, I get a message that reads: "Can't add script behaviour Player_Movement. The script needs to derive from MonoBehaviour!"

    All of my code worked fine before switching the platform to Android. I don't know if I have to do things differently with the code for Android or not, but this has left me clueless!

    Any help would be greatly appreciated!

    SPECS: Unity 5.3.2f1 (Personal) | Windows 8.1 (PC OS) | Android 4.1.2 (Phone OS)

    - Chris
     
  2. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    NOTE: I write the variables as follows:

    Code (JavaScript):
    1. var isPlayerDead : boolean;
    And I call a variable from another script as follows:

    Code (JavaScript):
    1. function Update ()
    2. {
    3.      isPlayerDead = GetComponent ("Player_Collisions").isPlayerDead;     // "Player_Collisions" script is another script attached to the Player object.
    4. }
    All code is written in UnityScript and both scripts "Player_Movement" and "Player_Collisions" are attached to the Player object.
     
    Last edited: Mar 21, 2016
  3. Omni_Owl

    Omni_Owl

    Joined:
    Nov 26, 2013
    Posts:
    176
    Could you perhaps show us the error log?
     
  4. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    I'm getting 979 errors, so I can't list them all out, but here's an example of an error I get:

    Assets/Scripts/Characters/Player_Movement.js(38,67): BCE0019: 'isPlayerDead' is not a member of 'UnityEngine.Component'.

    Here is the script associated with that error from the Player_Movement script attached to the Player object:

    Code (JavaScript):
    1. var isPlayerDead : boolean;
    2.  
    3. function Awake ()
    4. {
    5.      isPlayerDead = false;
    6. }
    7.  
    8. function Update ()
    9. {
    10.      isPlayerDead = GetComponent ("Player_Collisions").isPlayerDead;
    11. }
    And here is the code for the Player_Collisions script also attached to the Player object that involves the isPlayerDead variable:

    Code (JavaScript):
    1. var isPlayerDead : boolean;
    2.  
    3. function Awake ()
    4. {
    5.      isPlayerDead = false;
    6. }
    Now about 80% of all compiler errors I'm getting are for variables not being parts of components while the other 20% are like this:

    Assets/Scripts/Others/Play_State.js(86,50): BCE0019: 'isActive' is not a member of 'UnityEngine.GameObject'.

    There aren't even any objects that use the Play_State script right now, so I don't even know why that's triggering a compiler error while not in use.

    So basically, what it seems to come down to is that there are no such things as components or game objects in Android builds... Or they go by a different name that I am unaware of.

    PS: I have an error for every component and game object variable in every script (including inactive objects and unused scripts).
     
    Last edited: Mar 21, 2016
  5. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    I want to clarify that I only get these compiler errors when I build to Android. Things work fine for WebGL and Standalone.
     
  6. Omni_Owl

    Omni_Owl

    Joined:
    Nov 26, 2013
    Posts:
    176
    I wonder if you'd get the same errors if it was written with C#
     
  7. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    This shouldn't compile because see what GetComponent() returns
    isPlayerDead = GetComponent ("Player_Collisions").isPlayerDead;

    You can fix in 2 ways:
    Cast it to your script type
    isPlayerDead = (Player_Collisions)GetComponent ("Player_Collisions").isPlayerDead;
    Or use generics (the efficient way, not going through string lookups)
    isPlayerDead = GetComponent<Player_Collisions>().isPlayerDead;
     
    Chris-Ramer likes this.
  8. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    SOLVED!

    I did the later before reading your post. It turns out that all I had to do was remove the quotes from within the parenthesis like this:

    isPlayerDead = GetComponent (Player_Collisions).isPlayerDead.

    Thank you all for the help! Amazing how much frustration just a single pair of quotes can cause. :p