2016-04-07 20 views
-3

發生在所有出現的'{'和')'錯誤 我沒有看到任何錯誤?在類,結構體或接口成員聲明中出現無效的令牌')'和'{'

// Kills the game object 
Destroy{}(gameObject); 

// Removes this script instance from the game object 
//Destroy(this); 

// Removes the rigidbody from the game object 
Destroy{}(rigidbody); 

// Kills the game object in 5 seconds after loading the object 
Destroy{}(5, gameObject); 

// When the user presses Ctrl, it will remove the script 
// named FooScript from the game object 
void Update() 
{ 
    if (Input.GetButton("Fire1")) 
     GetComponent<FooScript>(); 
    { 
     Destroy(GetComponent<MonoScript>()); 
    } 
} 
+2

爲什麼你認爲你需要'{}'?只需在所有情況下刪除'{}',它就會起作用。 'Destroy {}(gameObject);'應該是'Destroy(gameObject);' – Rob

+1

...除非在'void Update()'中。 :-) – LarsTech

+0

@LarsTech是的,我想我不清楚 - 我的意思是所有的具體'{}'的情況下,通常不是大括號:) – Rob

回答

0

所有這些Destroy方法應該有一些方法的一部分。
他們不能像這樣在類中晃來晃去。
另外,{}用於打開新的作用域時,而不是在調用方法時使用。該代碼應該看起來像這樣:

// What are these? They should be in some method 
void DestroyAll() 
{ 
    // Kills the game object 
    Destroy(gameObject); 

    // Removes this script instance from the game object 
    //Destroy(this); 

    // Removes the rigidbody from the game object 
    Destroy(rigidbody); 

    // Kills the game object in 5 seconds after loading the object 
    Destroy(5, gameObject); 
} 

// When the user presses Ctrl, it will remove the script 
// named FooScript from the game object 
void Update() 
{ 
    if (Input.GetButton("Fire1")) 
    { 
     Destroy(GetComponent<FooScript>()); 
    } 
} 
相關問題