2016-05-26 6 views
0

私は死と呼ばれるスクリプトを持っています。このスクリプトは、衝突が真であるときに最初の位置にプレーヤーを再現します。私は、この衝突が真実であればマイナス100ポイントだが成功していないというスコアカウントを試みている。スクリプトは、スコアと死のスクリプトからの場合は、怒っている。どんな助けでも大歓迎です。Unityスクリプトのグローバル変数

スコアスクリプト:

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

** update **機能では、スコアを設定する前に** if(death)**チェックを入れてください。 –

答えて

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はかなり古く、すでにUnityバージョン4.6
  • 駅ので、新しいUIシステムに置き換えられましたティック変数は長期的にはスマートではなく、統計の仕組みが非常にわからない限り、オブジェクトのインスタンスを好む
  • これはイベントの使用例の良い例です。繰り返しますが、静的であると問題につながる可能性がありますが、最初の例では最も簡単です。
  • Unity Learnサイトでは、「スクリプト間のコミュニケーション」などのプログラミングの概念に関するチュートリアルが多数提供されています。また、完全なプロジェクトに沿った基本的なゲームの例もあります。
+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

もう一度ありがとうございます。ちょうど私がこれをフォーマットする方法をちょうど彷徨うと私はそれを1つのスクリプトに入れ、それは死のスクリプトだけです、スコアスクリプトでそれをどのように参照して、衝突が真であればスコアがマイナス100 。 –

+0

あなたは死のスクリプトにあなたが望むなら、あなたはパブリッククラスの死を守ることができます。あるいは、あなたは別のスクリプトを作り、それをそこに張ることができます。あなたはそれを持っていた方法を処理し、あなたがチェックが起こるようにするオブジェクト上に置く。クラス名で静的変数を参照するようにしてください。また、この回答が助けになった場合は、アップヴォートを与えて正解としてマークしてもよろしいですか? :) –

+0

ああ、あなたが実際にスコアのテキストにあなたが望む値を与えていることを確認し、テキストに "Score" + scoreに等しいと教えてください。 –