[英]How to rewrite this script to C# (Unity)?
我是一个新的 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' does not contain a definition for
GetComponent' object' does not contain a definition for
并且找不到GetComponent' of type
对象'的扩展方法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;
}
}
}
如果我使用新的 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
EDIT2:我注意到由于某种原因我没有看到目标,请在 Teleport 脚本组件的检查器中调整变量。
EDIT3:现在我可以毫无错误地运行它(我犯了一些小错误),但它对我不起作用。 如果我带着角色去“门户”,那么什么都不会发生。 有什么问题? 我已将检查器中的另一个“门户”添加到脚本中。
用这个。
public GameObject target;
target.GetComponent<Teleport>().jump = true;
参考这个。
也纠正这个
other.gameObject.transform.position = new Vector2 (target.position.x, target.position.y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.