C# 启动/更新变量范围
我有一个场景控制器,它应该初始化一组空的游戏对象产卵器,每个产卵器以相同的节奏一起工作。岩石产卵器接收一系列时间延迟,并在产卵另一块岩石之间等待X秒 当产卵器启动时,我设置了C# 启动/更新变量范围,c#,unity3d,scope,C#,Unity3d,Scope,我有一个场景控制器,它应该初始化一组空的游戏对象产卵器,每个产卵器以相同的节奏一起工作。岩石产卵器接收一系列时间延迟,并在产卵另一块岩石之间等待X秒 当产卵器启动时,我设置了\u nextSpawn=float.maxValue,并计划在“初始化”它们之后覆盖它(我自己的方法),但是即使我的调试日志说我在初始化时覆盖了我的\u nextSpawn值,更新循环仍在报告float.maxValue,没有任何结果生成,因为\u timesincelastprown未超过float.maxValue秒
\u nextSpawn=float.maxValue
,并计划在“初始化”它们之后覆盖它(我自己的方法),但是即使我的调试日志说我在初始化时覆盖了我的\u nextSpawn
值,更新循环仍在报告float.maxValue
,没有任何结果生成,因为\u timesincelastprown
未超过float.maxValue
秒
我的\u nextSpawn
变量的作用域是否缺少什么?至少乍一看,这似乎不是一个“这个”与“本地”的问题
调试输出:0 0 3 3
。0保持不变,3将根据rng而变化
SceneController.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneController : MonoBehaviour {
[SerializeField] private GameObject _rockSpawnerPrefab;
public int numRocks = 6;
public int minSpawnDelaySec = 1;
public int maxSpawnDelaySec = 3;
private bool spawnersInitialized = false;
void Start () {
InitializeSpawners();
}
void Update () {
}
void InitializeSpawners() {
float[] pattern = new float[numRocks];
for (int i = 0; i < numRocks; i++) {
// Generate delays at half second increments within bounds
float delay = Mathf.Floor(Random.value * ((float)(maxSpawnDelaySec + 0.5f - minSpawnDelaySec) / 0.5f));
delay = delay * 0.5f + minSpawnDelaySec;
pattern[i] = delay;
}
GameObject spawner = Instantiate(_rockSpawnerPrefab) as GameObject;
spawner.transform.position = new Vector3(0, 4, 0);
RockSpawner rockSpawnerScript = spawner.GetComponent<RockSpawner>();
rockSpawnerScript.Initialize(pattern);
}
}
这不是范围界定,而是通话顺序。当你创建一个
GameObject
its时,而不是当对象被创建时。因此,您的代码将首先调用Initialize
,然后调用Start
,这将覆盖这些值
删除
Start
中的代码,并处理Initialize
中的所有内容,它应该可以正常工作 这不是范围界定,而是通话顺序。当你创建一个GameObject
its时,而不是当对象被创建时。因此,您的代码将首先调用Initialize
,然后调用Start
,这将覆盖这些值
删除
Start
中的代码,并处理Initialize
中的所有内容,它应该可以正常工作 @SamiKuhmonen,抓得好。似乎在Start()之前调用了Initialize()。奇怪。我猜RockSpawner.Start()
可能在SceneController.Start()
完成之前不会被调用,这意味着所有的对象Start()可能会连续运行。@SamiKuhmonen,捕捉得好。似乎在Start()之前调用了Initialize()。奇怪。我猜在SceneController.Start()
完成之前,RockSpawner.Start()
可能不会被调用,这意味着所有对象Start()可能会连续运行。谢谢Sami。调试日志确认启动前已调用初始化。删除启动逻辑并在顶部设置_currentPattern=-1修复了该问题。谢谢你的帮助!谢谢你,萨米。调试日志确认启动前已调用初始化。删除启动逻辑并在顶部设置_currentPattern=-1修复了该问题。谢谢你的帮助!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RockSpawner : MonoBehaviour {
[SerializeField] private GameObject _rockPrefab;
public float minSpawnDelay = 3f;
public float maxSpawnDelay = 6f;
private float[] _pattern;
private int _currentPattern;
private float _timeSinceLastSpawn;
private float _nextSpawn;
void Start () {
_currentPattern = -1;
_nextSpawn = float.MaxValue;
}
void Update () {
if (_pattern == null) return;
_timeSinceLastSpawn += Time.deltaTime;
if (_timeSinceLastSpawn > _nextSpawn) {
GameObject rock = Instantiate(_rockPrefab) as GameObject;
rock.transform.position = transform.position;
NextTimer();
}
}
public void Initialize(float[] pattern) {
_pattern = pattern;
NextTimer();
}
private void NextTimer() {
_timeSinceLastSpawn = 0;
_currentPattern += 1;
Debug.Log(_nextSpawn);
Debug.Log(this._nextSpawn);
this._nextSpawn = _pattern[_currentPattern];
Debug.Log(_nextSpawn);
Debug.Log(this._nextSpawn);
}
}