c# - 如何将此脚本重写为 C# (Unity)?

标签 c# unity3d unityscript

我是 Unity 的新用户,我只是在 Unity 2D 游戏示例中尝试一些脚本。现在想做一个游戏的传送门,在网上找了一个脚本,不过是用UnityScript写的,但是我的游戏是用C#写的,所以想写成C#。我的 getComponent 方法有问题,因为如果我像在 JS 中那样使用它,就会出错。想请教各位,GetComponent应该写什么,或者怎么写。

这是 JS 脚本:

var target : GameObject;
var adjust : float;
var jump   : boolean;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

function OnTriggerEnter(other : Collider) {
    if (!jump) {
        if (other.tag == "Player") {
            target.GetComponent(Teleport).jump = true;
            other.gameObject.transform.position = Vector2 (target.position.x, target.position.y);
        }
    }
}

function OnTriggerExit(other : Collider) {
    if (other.tag == "Player") {
        jump = false;
    }
}
}

我在下一个代码中遇到了这些错误:

    public float adjust;
    public object target;
    public bool jump;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter(Collider other) {
    if (!jump) {
        if (other.tag == "Player") {
            target.GetComponent(Teleport).jump = true;
            other.gameObject.transform.position = Vector2 (target.position.x, target.position.y);
        }
    }
}

void OnTriggerExit(Collider other) {
    if (other.tag == "Player") {
        jump = false;
    }
}
}

类型 object' 不包含 GetComponent' 的定义,并且找不到类型为 object' 的扩展方法 GetComponent'(您是否缺少 using 指令或装配引用?)

表达式表示一个类型',其中需要一个变量'、值'或方法组'

如果您能帮助我,也许可以使用这段代码的 C# 脚本,那就太好了。

编辑:

I have tried what you said so my Teleport script looks like this now:

public class Teleport : MonoBehaviour {
public float adjust;
public GameObject target;
public bool jump;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter(Collider other) {
    if (!jump) {
        if (other.tag == "Player") {
            target.GetComponent<Teleport>.jump = true;
            other.gameObject.transform.position = Vector2 (target.position.x, target.position.y);
        }
    }
}

void OnTriggerExit(Collider other) {
    if (other.tag == "Player") {
        jump = false;
    }
}

如果我使用新的 Vector2,则会出现 5 个错误:

- Expression denotes a `method group', where a `variable', `value' or `type' was expected
- Type `UnityEngine.GameObject' does not contain a definition for `position' and no      extension method `position' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
- Type `UnityEngine.GameObject' does not contain a definition for `position' and no extension method `position' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
-  The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments
- Argument `#1' cannot convert `object' expression to type `float'

如果没有 new 运算符,我会遇到两个错误:

- Expression denotes a `method group', where a `variable', `value' or `type' was expected
- Expression denotes a `type', where a `variable', `value' or `method group' was expected

编辑2: 我注意到由于某种原因我没有看到目标,请在 Teleport 脚本组件的检查器中调整变量。

编辑3: 现在我可以毫无错误地运行它(我犯了一些小错误),但它对我不起作用。如果我和角色一起去“门户”,那么什么也不会发生。有什么问题?我已将检查器中的另一个“门户”添加到脚本中。

最佳答案

使用这个。

public GameObject target;

target.GetComponent<Teleport>().jump = true;

引用this .

也更正此问题

other.gameObject.transform.position = new Vector2 (target.position.x, target.position.y);

关于c# - 如何将此脚本重写为 C# (Unity)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21953751/

相关文章:

c# - 寻找一个属性(装饰)来提及与自动映射器一起使用的源属性名称

c# - Unity3D c#检查事件是否为空

c# - 需要将一些 UnityScript 转换为 C#

unit-testing - Unity 中的单元测试 - 获取 InvalidCastException - 为什么?以及如何解决?

c# - Unity3d进度条

unity-game-engine - 敌人逻辑错误

c# - AutoMapper 8.0 构造使用重大更改

c# - FileStreamResult 不返回主体

c# - 如何解决此错误 : No best type found for implicitly-typed array

c# - 订阅 Func 事件后不会调用协程方法