2016-05-26 43 views
0

我有一個腳本調用死亡,其重新滋生的開始位置播放器時的碰撞是真實的。我試圖計算一下,當這次碰撞是真的時,它會減去100分,但不成功。劇本咆哮,如果從比分和死亡腳本。任何幫助將非常感激。團結腳本全局變量

得分腳本:

var gui : GameObject; 
static var score : int; 
Death.death = false; 

function Start() 
{ 
    gui.GetComponent ("GUIText").text = "Score: 0"; 
} 


function Update() 
{ 
    gui.GetComponent ("GUIText").text = "Score: " + score; 
    if (death) 
    { 
     score = score - 100; 
    } 
} 

死亡腳本:

#pragma strict 
var Ball : Transform; 
public var death : boolean = false; 

function OnCollisionEnter (b : Collision) 
{ 
if (b.gameObject.tag == "Ball") 
    { 
    death = true; 
    Ball.transform.position.x = 1.6; 
    Ball.transform.position.y = 1.5; 
    Ball.transform.position.z = 1.1; 
    Ball.GetComponent.<Rigidbody>().velocity.y = 0; 
    Ball.GetComponent.<Rigidbody>().velocity.x = 0; 
    Ball.GetComponent.<Rigidbody>().velocity.z = 0; 
    } 
} 
+0

在** **更新功能,把**如果(死亡)**設定得分前檢查。 –

回答

1

我希望,我可以幫你,即使我使用C#。把它翻譯成UnityScript應該很容易。

using UnityEngine; 

public class Score : MonoBehaviour 
{ 
    public GUIText guiText; 
    int score; 

    void Update() 
    { 
     if(DeathTrigger.wasTriggered) 
     { 
      DeathTrigger.wasTriggered = false; 
      score -= 100; 
     } 
     guiText.text = string.Format("Score: {0}", score); 
    } 
} 

public class DeathTrigger : MonoBehaviour 
{ 
    public static bool wasTriggered; 

    void OnCollisionEnter(Collision other) 
    { 
     if (other.gameObject.CompareTag("Ball")) 
     { 
      wasTriggered = true; 
      // ... 
     } 
    } 
} 

我想這是一個初學者的問題,所以我不會說的變量是如何靜態是邪惡等什麼,但我還是要發佈的地方旁邊去一個更好的方法的例子:

using System; 
using UnityEngine; 
using UnityEngine.UI; 

public class BetterDeathTrigger : MonoBehaviour 
{ 
    // This event will be called when death is triggered. 
    public static event Action wasTriggered; 

    void OnCollisionEnter(Collision other) 
    { 
     if (other.gameObject.CompareTag("Ball")) 
     { 
      // Call the event. 
      if (wasTriggered != null) 
       wasTriggered(); 
      // ... 
     } 
    } 
} 

public class BetterScore : MonoBehaviour 
{ 
    public Text uiText; // Unity 4.6 UI system 
    int score; 

    void Start() 
    { 
     // Subscribe to the event. 
     BetterDeathTrigger.wasTriggered += WasTriggered_Handler; 
    } 

    // This method will now be called everytime the event is called from the DeathTrigger. 
    private void WasTriggered_Handler() 
    { 
     score -= 100; 
     uiText.text = string.Format("Score: {0}", score); 
    } 
} 

幾件事情:

  • GUIText是很老,因爲統一的版本已經被新的UI系統取代4.6
  • 站抽動變量不從長遠來看智能,更喜歡對象的情況下,除非你非常肯定靜是如何工作的
  • 這是在哪裏使用事件很好的例子。同樣,它是靜態的可能會導致問題,但對於第一個例子來說,它是最簡單的。
  • 統一性瞭解網站提供了很多關於編程概念,如「腳本之間的通信」的教程,他們也有基本的遊戲例子,你可以沿着一個完整的項目跟進。
+0

非常感謝。希望這會顯着改善我的比賽。 –

0

這絕對是值得一試什麼Xarbrough建議去代替。從長遠來看,靜力學可能會造成混亂並阻礙其發展。但是,這裏是你如何做到這一點,用Javascript編寫。

public class Death { // you can change the class name to something that is broad enough to hold several public static variables 
    public static var death : boolean; 
}//This will end this class. When you make public classes like this, the script doesnt even need to be attached to a object, because it doesn't use Mono behavior 

//這將是實際DeathScript,什麼上面是技術上沒有死腳本的一部分

var Ball : Transform; 

function OnCollisionEnter (b : Collision) { 
    if (b.gameObject.tag == "Ball"){ 
    Death.death = true; 
    Ball.transform.position.x = 1.6; 
    Ball.transform.position.y = 1.5; 
    Ball.transform.position.z = 1.1; 
    Ball.GetComponent.<Rigidbody>().velocity.y = 0; 
    Ball.GetComponent.<Rigidbody>().velocity.x = 0; 
    Ball.GetComponent.<Rigidbody>().velocity.z = 0; 
} } 

從那裏,任何時候你要訪問的靜態變量,只要告訴它在哪裏看。 Death.death。 希望這有助於!

+0

再次感謝。只是徘徊,我怎麼會格式化這我會把它放到一個腳本,這只是死亡腳本,我將如何指在評分腳本中說,當碰撞是真實的gui文本顯示任何分數是減100 –

+0

如果你願意,你可以在死亡劇本中加入公開課程,或者你可以製作一個單獨的腳本並粘貼在那裏。至於gui的東西,處理你如何擁有它,把它放在你希望支票發生的物體上。只要確保使用類名引用靜態變量即可。另外,如果這個答案有幫助,你會介意給予贊成並將其標記爲正確答案嗎? :) –

+0

噢,並確保你實際上給你的分數文本的價值,告訴文本等於「分數」+分數; –