Charles Reed
Charles Reed

Reputation: 33

Problems building unity code for android

My code works perfectly on windows, but when I switch to android it doesn't work. I get the error

Assets/enemyNo.js(49,20): BCE0019: 'gameObject' is not a member of 'Object'. 

Here is the code I'm having problems with

function OnTriggerEnter2D(obj) {

// Name of the object that collided with the enemy
var name = obj.gameObject.name;

// If the enemy collided with a bullet
if (name == "bullet(Clone)") {
    // Destroy itself (the enemy) and the bullet
    //shship.AddScore(scoreValue);
    Destroy(gameObject);
    Destroy(obj.gameObject);
}

// If the enemy collided with the spaceship
if (name == "spaceship") {
    // Destroy itself (the enemy) to keep things simple
    Destroy(gameObject);
}
}

My min android is set to kitkat, I commented it out and it works fine, there's just no interaction with the objects. My SDK wasn't originally working, but I copied part of the older SDK (tool I believe) and it can compile and turn into an APK

Upvotes: 0

Views: 138

Answers (1)

Andrea
Andrea

Reputation: 6125

Because Android doesn't support dynamic typing. In this line of code:

var name = obj.gameObject.name;

name is declared as a generic variable, without the specific type. Windows, during the compilation phase, let it pass, but Android will not. It should be:

var name : String = obj.gameObject.name;

Two hints:

1) [recommended] Start learning C#, it's more robust, efficient and better supported.

2) If you'd like to continue with the UnityScript path, add #pragma strict at the beginning of every script of yours. This force the compiler to apply the strict rules as in Android, preventing dynamic typing (compilation errors will arise on Windows as it would be on Android).

Upvotes: 1

Related Questions