Unity Scripting全局变量

时间:2016-05-26 00:11:27

标签: javascript unity3d scripting

我有一个名为Death的脚本,当碰撞为真时,它会在起始位置产生玩家。我试图计算得分,当这次碰撞为真时,它将减去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;
    }
}

2 个答案:

答案 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相当陈旧,自Unity版本4.6以来已被新的UI系统取代。
  • 从长远来看,静态变量并不聪明,更喜欢对象的实例,除非您非常确定静态变量如何工作
  • 这是使用事件的好地方。同样,它是静态的可能会导致问题,但对于第一个例子,它是最简单的。
  • Unity Learn网站提供了许多关于编程概念的教程,例如&#34;脚本之间的通信&#34;,它们还有基本的游戏示例,您可以在其中完成整个项目。

答案 1 :(得分: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。 希望这有帮助!