2017-07-21 7 views
-1

初めて実際にプロパティを実装しようとしていますが、私が気付いていない基本的なものが不足しているようです。私は一度スクリプトから別の値にbool値を渡して、それを常にUpdate()を使って上げようとしています。プロパティが他のクラスに渡されない

GameManager.cs私はプレーヤーのアイドル状態をチェックしています。プレイヤーがアイドル状態になると、プロパティーはUserActiveに設定されます。 PreCountdownTimer.csで私はちょうど_userActiveが更新しているかどうかを確認するためにテストしていますが、そうではありません。 GameManager()のプロパティ値がPreCountdownTimer()に渡されないのはなぜですか?

GameManager.cs

using UnityEngine; 
using UnityEngine.SceneManagement; 

public class GameManager : MonoBehaviour 
{ 
    public static GameManager Instance = null; 

    public Object introScene; 

    private bool _userActive; 
    public bool UserActive { get; set; } 
    private bool _onIntroScreen; 
    public bool OnIntroScreen { get; set; } 

    public GameObject preCountdownTimerPrefab; 
    private GameObject _preCountdownTimerInstance; 
    private float _preCountdownLength; 
    public float PreCountdownLength { get; protected set; } 
    private float _preCountdownInterval; 
    public float PreCountdownInterval { get; protected set; } 

    private float _checkMousePositionTimingInterval = 0.1f; 
    private Vector3 _currentMousePosition; 
    private Vector3 _prevMousePosition; 
    private Scene _currentScene; 

    void Awake() 
    { 
     if (Instance == null) 
      Instance = this; 
     else if (Instance != null) 
      Destroy(gameObject); 
     DontDestroyOnLoad(gameObject); 
    } 

    void OnEnable() 
    { 
     SceneManager.sceneLoaded += OnSceneLoaded; 
    } 

    void OnSceneLoaded(Scene scene, LoadSceneMode mode) 
    { 
     _currentScene = scene; 
    } 

    void Start() 
    { 
     PreCountdownLength = 5.0f; 
     PreCountdownInterval = 1.0f; 

     _onIntroScreen = true; 
     _userActive = false; 

     _prevMousePosition = Input.mousePosition; 

     InvokeRepeating("LastMousePosition", 0, _checkMousePositionTimingInterval); 
    } 

    void Update() 
    { 
     _currentMousePosition = Input.mousePosition; 

     // CHECK FOR PLAYER IDLE 
     if (_currentScene.name != introScene.name) 
     { 
      _onIntroScreen = false; 

      if (_currentMousePosition != _prevMousePosition) 
      { 
       _userActive = true; 
      } 
      else 
      { 
       _userActive = false; 
      }  
     } 
     else if (_currentScene.name == introScene.name) 
     { 
      _onIntroScreen = true; 
     } 

     // IF IDLE START PRE-COUNT TIMER ELSE DESTROY 
     if (!_userActive && !_onIntroScreen) 
     { 
      if (_preCountdownTimerInstance == null) 
       _preCountdownTimerInstance = Instantiate(preCountdownTimerPrefab); 
     } 
     else if (_userActive) 
     { 
      if (_preCountdownTimerInstance != null) 
       Destroy(_preCountdownTimerInstance); 
     } 
    } 

    void LastMousePosition() 
    { 
     _prevMousePosition = Input.mousePosition; 
    } 
} 

PreCountdownTimer.csあなたは、フィールドやプロパティを持っているGameManager

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class PreCountdownTimer : MonoBehaviour { 

    private IEnumerator preCounter; 

    private float _preCountdownInterval; 
    private bool _preCountdownActive; 
    private bool _userActive; 
    private bool _onIntroScreen; 
    private float _timerLength; 

    void Start() 
    { 
     _timerLength = GameManager.Instance.PreCountdownLength; 
    } 

    void Update() 
    { 
     _userActive = GameManager.Instance.UserActive; 
     _onIntroScreen = GameManager.Instance.OnIntroScreen; 

     Debug.Log("User Activity: " + _userActive); 
    } 
} 
+0

どこからPreCountdownTimer.Updateメソッドが呼び出されていますか? – Andrew

答えて

2

private bool _userActive;    // field 
public bool UserActive { get; set; } // property 

者が共同ではありません接続されています。 _userActiveと設定すると、UserActiveには影響しません。 PreCountdownTimerでは、GameManager.Instance.UserActiveをチェックします。 (現在のフィールドを必要としないので、私は、この方法を好む)

private bool _userActive; 
public bool UserActive 
{ 
    get 
    { 
     return _userActive; 
    } 
    set 
    { 
     _userActive = value; 
    } 
} 

またはすべてのフィールド_userActiveを削除し、単にプロパティを設定します:修正がいずれかのフィールドとプロパティを接続することである

void Start() 
{ 
    PreCountdownLength = 5.0f; 
    PreCountdownInterval = 1.0f; 

    _onIntroScreen = true; 
    UserActive = false; 

    _prevMousePosition = Input.mousePosition; 

    InvokeRepeating("LastMousePosition", 0, _checkMousePositionTimingInterval); 
} 

void Update() 
{ 
    _currentMousePosition = Input.mousePosition; 

    // CHECK FOR PLAYER IDLE 
    if (_currentScene.name != introScene.name) 
    { 
     _onIntroScreen = false; 

     if (_currentMousePosition != _prevMousePosition) 
     { 
      UserActive = true; 
     } 
     else 
     { 
      UserActive = false; 
     }  
    } 
    else if (_currentScene.name == introScene.name) 
    { 
     _onIntroScreen = true; 
    } 

    // IF IDLE START PRE-COUNT TIMER ELSE DESTROY 
    if (!UserActive && !_onIntroScreen) 
    { 
     if (_preCountdownTimerInstance == null) 
      _preCountdownTimerInstance = Instantiate(preCountdownTimerPrefab); 
    } 
    else if (UserActive) 
    { 
     if (_preCountdownTimerInstance != null) 
      Destroy(_preCountdownTimerInstance); 
    } 
} 

GameManagerの他のプロパティでも同じ問題が発生します。また、私はフィールドの代わりに(GameManagerの)プロパティで作業することをお勧めします。このようにして、何が起こっているのかをより詳細に制御できます。絶対に必要なときには、フィールドで直接作業してください。

+0

ユーザーがアクティブなときに 'PreCountdownTimer'を破壊しないでください?私はこのコードを意味します: 'else if(_userActive) { if(_preCountdownTimerInstance!= null) Destroy(_preCountdownTimerInstance); } ' –

1

GameManagerでは、あなたが最初のプライベートフィールドです

private bool _userActive; 
public bool UserActive { get; set; } 

が定義されている、第二は公共財産である一方で、彼らは似た名前を持つが、それらは両方とも独立しているので、あなたはそれらを一緒にリンクしていません。

は、次の2つのオプション、

1)パブリックプロパティは、このような第二の選択肢がある

private bool _userActive; 
public bool UserActive 
{ 
    get 
    { 
     return _userActive; 
    }; 
    set 
    { 
     this._userActive = value; 
    }; 
} 

以下のようにプライベートフィールドを使用してくださいプライベート_userActiveを削除し、唯一の公共財産 2)を使用していいくつかの検証、再フォーマット、または他のアクションのトリガと値の取得または設定が必要な場合に最も便利です。

+0

申し訳ありませんが、私は同時に書いていましたが、私が投稿するまであなたの答えは見えませんでした! –

関連する問題