Javascript 为android构建统一代码时遇到的问题

Javascript 为android构建统一代码时遇到的问题,javascript,android,unity3d,Javascript,Android,Unity3d,我的代码在windows上运行得很好,但当我切换到android时,它就不工作了。我得到了错误 Assets/enemyNo.js(49,20): BCE0019: 'gameObject' is not a member of 'Object'. 这是我遇到问题的代码 function OnTriggerEnter2D(obj) { // Name of the object that collided with the enemy var name = obj.gameObject.n

我的代码在windows上运行得很好,但当我切换到android时,它就不工作了。我得到了错误

Assets/enemyNo.js(49,20): BCE0019: 'gameObject' is not a member of 'Object'. 
这是我遇到问题的代码

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);
}
}

我的minandroid设置为kitkat,我把它注释掉了,它工作得很好,只是和对象没有交互。我的SDK最初不工作,但我复制了旧SDK的一部分(我相信是工具),它可以编译成APK,因为Android不支持动态键入。 在这一行代码中:

var name = obj.gameObject.name;
name被声明为泛型变量,没有特定类型。 Windows在编译阶段允许它通过,但Android不会。 应该是:

var name : String = obj.gameObject.name;
两个提示:

1) [推荐]开始学习C#,它更强大、更高效、支持更好


2) 如果要继续使用UnityScript路径,请在每个脚本的开头添加
#pragma strict
。这迫使编译器像在Android中一样应用严格的规则,防止动态键入(编译错误会在Windows上出现,就像在Android上一样)。

“我的代码在Windows上运行得很好,但当我切换到Android时它就不工作了”你确定吗?它甚至不应该在Windows上工作。这应该是OnTiggerEnter2D(obj:CollizedR2D)的
函数
而不是
函数OnTiggerEnter2D(obj){
。请注意,Javascript现在正在运行。你需要开始使用C#。这是修复方法!非常感谢!代码在windows上以这种奇怪的方式完全工作。我压力过大,这是一个针对类的游戏,但不起作用。现在一切都好了!我一直想学习C#,只是一直没有找到时间。谢谢你的输入。这是一个课堂上的项目,我想有一天学习C#,但现在javascript已经足够了。你的评论以及需要修改原型以包含“CollizedR2D”的内容修复了它!