【问题标题】:How to rewrite this script to C# (Unity)?如何将此脚本重写为 C# (Unity)?
【发布时间】:2014-02-22 11:38:12
【问题描述】:

我是 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 forGetComponent' 并且找不到扩展方法 GetComponent' of typeobject'(您是否缺少 using 指令或程序集引用?)

表达式表示type', where avariable',应为value' ormethod group'

如果你能帮助我,也许可以使用这段代码的工作 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: 现在我可以毫无错误地运行它(我犯了一些小错误),但它对我不起作用。如果我和角色一起去“门户”,那么什么也不会发生。有什么问题?我已将检查器中的另一个“门户”添加到脚本中。

【问题讨论】:

  • 尝试在触发器中获取日志以检查您是否进入其中。

标签: c# unity3d unityscript


【解决方案1】:

使用这个。

public GameObject target;

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

请参考this

也可以用

纠正这个问题
other.gameObject.transform.position = new Vector2 (target.position.x, target.position.y);

【讨论】:

  • 另外我认为目标类型需要从对象更改为 GameObject,因为稍后在代码中目标试图调用对象不可用的 GetComponent 方法。我说的对吗?
猜你喜欢
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
  • 2022-11-24
  • 1970-01-01
相关资源
最近更新 更多