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

Zwi*_*bel 1 c# unity-game-engine

我是一个新的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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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

    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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

键入object' does not contain a definition forGetComponent'并且找不到扩展方法GetComponent' of type对象'(您是否缺少using指令或程序集引用?)

表达式表示type', where a变量',value' or方法组'是预期的

如果你可以帮助我,也许使用这段代码的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

如果我使用新的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'
Run Code Online (Sandbox Code Playgroud)

没有新的运算符我有两个错误:

- 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
Run Code Online (Sandbox Code Playgroud)

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

编辑3:现在我可以毫无错误地运行它(我犯了小错误),但它对我不起作用.如果我带着角色进入"门户",那么没有任何反应.有什么不对?我已将检查器中的另一个"门户"添加到脚本中.

Nic*_*ick 5

用这个.

public GameObject target;

target.GetComponent<Teleport>().jump = true;
Run Code Online (Sandbox Code Playgroud)

请参阅.

也纠正这个

other.gameObject.transform.position = new Vector2 (target.position.x, target.position.y);
Run Code Online (Sandbox Code Playgroud)