【问题标题】:Unity Scripting global variableUnity 脚本全局变量
【发布时间】:2016-09-23 19:00:05
【问题描述】:

我有一个名为 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;
    }
}

【问题讨论】:

  • update函数中,在设置分数之前把if (death)检查一下。

标签: javascript unity3d scripting


【解决方案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 网站提供了许多有关编程概念的教程,例如“脚本之间的通信”,它们还包含基本的游戏示例,您可以在其中学习完整的项目。

【讨论】:

  • 非常感谢。希望这会极大地改善我的游戏。
【解决方案2】:

绝对值得尝试 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

//这将是真正的死亡脚本,上面的内容在技术上不是死亡脚本的一部分

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;
 } }

从那里,只要您想访问静态变量,只需告诉它在哪里查找即可。死亡。死亡。 希望这会有所帮助!

【讨论】:

  • 再次感谢。只是徘徊我将如何格式化它,我会将它放入一个脚本中,而这只是死亡脚本,我将如何在分数脚本中引用它来说明当碰撞为真时,gui文本显示分数为负 100 .
  • 如果你愿意,你可以在你的死亡脚本中粘贴公共类Death,或者你可以制作一个单独的脚本并粘贴在那里。至于 gui 的东西,处理你如何拥有它,把它放在你想要检查的对象上。只需确保使用类名引用静态变量即可。另外,如果这个答案有帮助,您介意投赞成票并将其标记为正确答案吗? :)
  • 哦,请确保您实际上为分数文本提供了您想要的值,告诉文本等于“分数”+分数;
猜你喜欢
  • 2016-03-03
  • 2022-01-21
  • 1970-01-01
  • 2013-10-06
  • 2021-10-19
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多