繁体   English   中英

如何使用C#在Unity中的两个脚本之间访问更新的变量?

[英]How do I access an updated variable between two scripts in Unity with C#?

希望这不是太多细节,我不习惯问编程问题。

我正在尝试使用Udemy上的Unity 3D课程进行3D视频游戏开发,尽管使用的是C#而不是Javascript。 我刚刚完成了有关创建太空射击游戏的教程。

其中,用户按下按钮时会创建一个盾牌。 防护罩具有“使用次数”变量,在本教程完成时实际上并未使用。 我正在尝试添加它,并成功地实现了它,因此每次使用时,我们都会减少剩余的使用次数,并且一旦该次数<= 0,就不再能够实例化防护。

此变量存储在播放器中,如果我从播放器中打印出来,它将返回我期望的值。

但是,我使用了一个单独的SceneManager.cs(在此教程中放置了生命,得分和计时器变量),在该处将数字打印到GUI中。 我的问题是,当我尝试从场景管理器打印它时,我无法使我的use变量数量保持最新状态……它注册了初始值,但此后不更新。

这是播放器脚本

using UnityEngine;
using System.Collections;

public class player_script : MonoBehaviour {

    // Inspector Variables
    public int numberOfShields  = 2;        // The number of times the user can create a shield
    public Transform shieldMesh;                // path to the shield
    public KeyCode shieldKeyInput;              // the key to activate the shield
    public static bool shieldOff    = true;     // initialize the shield to an "off" state

    public int NumberOfShields
    {
        get{return numberOfShields;}
        set{numberOfShields = value;}
    }

    // Update is called once per frame
    void Update()
    {
        // create a shield when shieldKey has been pressed by player
        if (Input.GetKeyDown (shieldKeyInput)) {
            if(shieldOff && numberOfShields>0)
            {
                // creates an instance of the shield
                Transform clone;
                clone = Instantiate (shieldMesh, transform.position, transform.rotation) as Transform;

                // transforms the instance of the shield
                clone.transform.parent = gameObject.transform;

                // set the shield to an on position
                shieldOff = false;

                // reduce the numberOfShields left
                numberOfShields -=1;

            }
        }

        print ("NumberOfShields = " + NumberOfShields);
    }

    public void turnShieldOff()
    {
        shieldOff = true;
    }

}

当我运行“ print(” NumberOfShields =“ + NumberOfShields);” 我得到了我期望的价值。 (当星形与屏蔽碰撞时,它们会触发turnShieldOff()。

但是,在我的场景管理器中...这是我正在运行的代码:

using UnityEngine;
using System.Collections;

public class SceneManager_script : MonoBehaviour {

// Inspector Variables
public GameObject playerCharacter;
private player_script player_Script;
private int shields = 0;

// Use this for initialization
void Start ()
{
    player_Script = playerCharacter.GetComponent<player_script>();
}

// Update is called once per frame
void Update ()
{
    shields = player_Script.NumberOfShields;
    print(shields);
}

// GUI
void OnGUI()
{
    GUI.Label (new Rect (10, 40, 100, 20), "Shields: " + shields);
}
}

知道我做错了什么会阻止我的player_script中的NumberOfShields更新时,我的SceneManager脚本中的盾牌无法更新吗?

我认为您可能已将一个预制块分配给playerCharacter GameObject变量,而不是游戏单元中的实际变量。 在这种情况下,它将始终打印预设的默认屏蔽值。 而不是通过检查器分配该变量,而是尝试在Start函数中找到玩家GameObject。 例如,您可以给您的玩家对象一个标签,然后:

void Start() {
    playerCharacter = GameObject.FindGameObjectWithTag("Player");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM