我是新的Unity用戶,我只是在Unity 2D遊戲示例中嘗試一些腳本。現在我想創建一個遊戲門戶,並且我已經在互聯網上找到了一個腳本,但它已經用UnityScript編寫,但是我的遊戲是用C#編寫的,所以我想將它寫入C#。我有一個getComponent方法的問題,因爲如果我在JS中使用它,會得到錯誤。我想問你,我應該寫什麼來代替GetComponent,或者我應該如何編寫它。如何將此腳本重寫爲C#(Unity)?
這裏的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
對象' 可以找到(你缺少使用指令或組件參考?)
表達式表示變量'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: 我注意到由於某種原因,我沒有看到目標,請在傳送腳本組件中的檢查器中調整變量。
EDIT3: 現在我可以無誤地運行它(我犯了一些小錯誤),但它對我來說不起作用。如果我帶着角色去「門戶」,那麼什麼都不會發生。什麼可能是錯誤的?我已將檢查員中的另一個「門戶」添加到腳本中。
嘗試獲得觸發內部日誌,以檢查是否R內即或沒有得到。 – Nick