我是一名新的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'并且找不到扩展方法GetComponent' of type
object'(您是否缺少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'
如果没有新操作符,我会遇到两个错误:
- 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: 现在我可以毫无错误地运行它(我犯了小错误),但它对我不起作用。如果我带着角色进入“门户”,那么没有任何反应。有什么不对?我已将检查器中的另一个“门户”添加到脚本中。
答案 0 :(得分:5)
使用此功能。
public GameObject target;
target.GetComponent<Teleport>().jump = true;
参考this。
也可以使用
更正此问题other.gameObject.transform.position = new Vector2 (target.position.x, target.position.y);