检查点是否可见时出现 NullreferenceException
NullreferenceException when checking if point is visible
我知道可能存在重复项,我试图找出导致我的空值的原因,但也许你可以帮助我。所以我正在制作一个游戏,我在检查器中定义为数组的六个点随机生成 "PoliceMan"。这是由协程定期完成的。我试图添加一种方法来检查摄像机是否可以看到生成点。这是我的代码片段:
IEnumerator AddPoliceManCR()
{
int counter = 0;
yield return new WaitForSeconds(1.0f);
while (counter < 10)
{
AddPoliceMan();
counter++;
yield return new WaitForSeconds(Random.Range(3.0f, 5.0f));
}
while (true)
{
AddPoliceMan();
yield return new WaitForSeconds(Random.Range(0.5f, 3.0f));
}
}
void AddPoliceMan()
{
GameObject policeManClone = (GameObject)policemanPool.getPooledPoliceman();
policeManClone.transform.position = SetSpawnPosition();
policeManClone.transform.LookAt(new Vector3(player.transform.position.x, 0, player.transform.position.z));
policeManClone.tag = "PoliceMan";
policeManClone.SetActive(true);
}
Vector3 SetSpawnPosition()
{
Vector3 position;
position = spawnPositions[Random.Range(0, 6)];
Debug.Log(position.ToString());
if (Camera.current.WorldToViewportPoint(position).x > 0f
&& Camera.current.WorldToViewportPoint(position).x < 1f
&& Camera.current.WorldToViewportPoint(position).y > 0f
&& Camera.current.WorldToViewportPoint(position).y < 1f
&& Camera.current.WorldToViewportPoint(position).z > 0f)
{
Debug.Log("Spawn point visible");
}
else
{
Debug.Log("Spawn point is NOT visible");
}
return position;
}
协程在Start()
中说明。这个脚本和游戏对象在任何时候都不会被破坏。 NullReferenceException 符合:if (Camera.current.WorldToViewportPoint(position).x > 0f
Debug.Logs 无论点是否可见,至少在大多数情况下都能正确显示。
问题是每次调用 SetSpawnPopsition()
时都不会发生这种情况。可能是游戏立即开始,也可能是调用此方法 20 次后。似乎是随机的。另外,如果在为什么显示错误之前仅两行初始化点?
如果我将返回行 policeManClone.transform.position = SetSpawnPosition();
设置为 policeManClone.transform.position = spawnPositions[Random.Range(0, 6)];
我没有任何错误。
准确的错误是:
NullReferenceException: Object reference not set to an instance of an
object GameControl.SetSpawnPosition () (at
Assets/Scripts/GameControl.cs:140) GameControl.AddPoliceMan () (at
Assets/Scripts/GameControl.cs:67)
GameControl+c__Iterator0.MoveNext () (at
Assets/Scripts/GameControl.cs:92)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator,
IntPtr returnValueAddress) (at
C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
编辑
好吧,我想这可能是另一回事。看起来 Camera.current
给我 null。在将其更改为 public 变量并在检查器中分配主摄像头后,此错误似乎已得到纠正。问题是为什么 Camera.current 导致空值?
Ok, I had idea that it might be the other thing. Looks like
Camera.current is giving me null. After changing it to a public
variable with main camera assigned in inspector this error seems to be
corrected. Question is why is Camera.current causing null?
如果场景视图没有焦点,Camera.current
将是 return。您应该使用 Camera.main
而不是 Camera.current
。
此外,在使用 Camera.main
之前,请确保相机具有标签 MainCamera。这是在创建新场景时默认完成的,但您必须验证这一点,否则您将再次获得 null 异常。
我知道可能存在重复项,我试图找出导致我的空值的原因,但也许你可以帮助我。所以我正在制作一个游戏,我在检查器中定义为数组的六个点随机生成 "PoliceMan"。这是由协程定期完成的。我试图添加一种方法来检查摄像机是否可以看到生成点。这是我的代码片段:
IEnumerator AddPoliceManCR()
{
int counter = 0;
yield return new WaitForSeconds(1.0f);
while (counter < 10)
{
AddPoliceMan();
counter++;
yield return new WaitForSeconds(Random.Range(3.0f, 5.0f));
}
while (true)
{
AddPoliceMan();
yield return new WaitForSeconds(Random.Range(0.5f, 3.0f));
}
}
void AddPoliceMan()
{
GameObject policeManClone = (GameObject)policemanPool.getPooledPoliceman();
policeManClone.transform.position = SetSpawnPosition();
policeManClone.transform.LookAt(new Vector3(player.transform.position.x, 0, player.transform.position.z));
policeManClone.tag = "PoliceMan";
policeManClone.SetActive(true);
}
Vector3 SetSpawnPosition()
{
Vector3 position;
position = spawnPositions[Random.Range(0, 6)];
Debug.Log(position.ToString());
if (Camera.current.WorldToViewportPoint(position).x > 0f
&& Camera.current.WorldToViewportPoint(position).x < 1f
&& Camera.current.WorldToViewportPoint(position).y > 0f
&& Camera.current.WorldToViewportPoint(position).y < 1f
&& Camera.current.WorldToViewportPoint(position).z > 0f)
{
Debug.Log("Spawn point visible");
}
else
{
Debug.Log("Spawn point is NOT visible");
}
return position;
}
协程在Start()
中说明。这个脚本和游戏对象在任何时候都不会被破坏。 NullReferenceException 符合:if (Camera.current.WorldToViewportPoint(position).x > 0f
Debug.Logs 无论点是否可见,至少在大多数情况下都能正确显示。
问题是每次调用 SetSpawnPopsition()
时都不会发生这种情况。可能是游戏立即开始,也可能是调用此方法 20 次后。似乎是随机的。另外,如果在为什么显示错误之前仅两行初始化点?
如果我将返回行 policeManClone.transform.position = SetSpawnPosition();
设置为 policeManClone.transform.position = spawnPositions[Random.Range(0, 6)];
我没有任何错误。
准确的错误是:
NullReferenceException: Object reference not set to an instance of an object GameControl.SetSpawnPosition () (at Assets/Scripts/GameControl.cs:140) GameControl.AddPoliceMan () (at Assets/Scripts/GameControl.cs:67) GameControl+c__Iterator0.MoveNext () (at Assets/Scripts/GameControl.cs:92) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
编辑
好吧,我想这可能是另一回事。看起来 Camera.current
给我 null。在将其更改为 public 变量并在检查器中分配主摄像头后,此错误似乎已得到纠正。问题是为什么 Camera.current 导致空值?
如果场景视图没有焦点,Ok, I had idea that it might be the other thing. Looks like Camera.current is giving me null. After changing it to a public variable with main camera assigned in inspector this error seems to be corrected. Question is why is Camera.current causing null?
Camera.current
将是 return。您应该使用 Camera.main
而不是 Camera.current
。
此外,在使用 Camera.main
之前,请确保相机具有标签 MainCamera。这是在创建新场景时默认完成的,但您必须验证这一点,否则您将再次获得 null 异常。