双关语 2 |无法动态添加玩家子变换以变换数组
PUN 2 | Cant dynamically add players child transform to transform array
我正在为所有玩家使用以下相机脚本 (link)。脚本放大和缩小以捕获所有玩家。游戏中共有 4 名玩家。现在用 2 测试。我无法让网络播放器的子对象(Kitty_Orange 的转换)自动附加到相机。
子对象具有 Player 标签。
https://learn.unity.com/tutorial/camera-control?projectId=5c5149c5edbc2a001fd5be95#5c7f8528edbc2a002053b398
我有一个 GameSetupController.cs 可以将玩家实例化到场景中。这似乎是将头像转换添加到相机的最合适的地方。当玩家进入游戏场景时,我得到一个空引用。
尝试向相机 m_Targets 变换数组动态添加变换时出错。
有调试
Debug.Log("CC.m_Targets.Length" + CC.m_Targets.Length); //Troubleshooting
CC.m_Targets = new Transform[players.Length]; // array of size 1-4
NullReferenceException: Object reference not set to an instance of an object
GameSetupController.CreatePlayer () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:33)
GameSetupController.Start () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:14)
没有调试
CC.m_Targets = new Transform[players.Length]; // array of size 1-4
NullReferenceException: Object reference not set to an instance of an object
GameSetupController.CreatePlayer () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:34)
GameSetupController.Start () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:14)
CameraControl.cs
public Transform[] m_Targets; // All the targets the camera needs to encompass. [HideInInspector]
GameSetupController.cs
using Photon.Pun;
using System.IO;
using UnityEngine;
public class GameSetupController : MonoBehaviour
{
private CameraControl CC;
public GameObject[] players;
// This script will be added to any multiplayer scene
void Start()
{
CC = GetComponent<CameraControl>();
CreatePlayer(); //Create a networked player object for each player that loads into the multiplayer scenes.
}
private void CreatePlayer()
{
Debug.Log("Creating Player");
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), Vector3.zero, Quaternion.identity);
players = GameObject.FindGameObjectsWithTag("Player");
if (players.Length == 0)
{
return;
}
for (int i = 0; i < players.Length; i++)
{
Debug.Log("players.Length" + players.Length); //Troubleshooting
Debug.Log("CC.m_Targets.Length" + CC.m_Targets.Length); //Troubleshooting
CC.m_Targets = new Transform[players.Length]; // array of size 1-4
Debug.Log(CC.m_Targets.Length);
Debug.Log(players.Length);
CC.m_Targets[i] = players[i].transform;
Debug.Log("m_Targets : " + CC.m_Targets[i]);
Debug.Log("players : " + players[i]);
}
}
}
我的临时解决方案 - 在 CameraControl.cs
内
private void FixedUpdate()
{
m_Targets = new List<Transform>();
//ADDS PLAYERS TO THE M_TARGETS LIST!
players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject child in players)
{
//Debug.Log(child.gameObject.transform.GetChild(0));
m_Targets.Add(child.gameObject.transform.GetChild(0));
}
您的 CC 对象为空,这就是空引用的来源。 GetComponent
只会找到与调用它的脚本附加到同一游戏对象的组件。如果对象是在编辑器中静态添加到场景中的,则可以使用 [SerializeField]
属性使私有对象在检查器中可见并手动分配引用。 Find
方法通常很昂贵,因此如果您可以通过将引用存储在公共位置或预先设置它们来避免使用它们,那么这是推荐的方法。
我正在为所有玩家使用以下相机脚本 (link)。脚本放大和缩小以捕获所有玩家。游戏中共有 4 名玩家。现在用 2 测试。我无法让网络播放器的子对象(Kitty_Orange 的转换)自动附加到相机。
子对象具有 Player 标签。
https://learn.unity.com/tutorial/camera-control?projectId=5c5149c5edbc2a001fd5be95#5c7f8528edbc2a002053b398
我有一个 GameSetupController.cs 可以将玩家实例化到场景中。这似乎是将头像转换添加到相机的最合适的地方。当玩家进入游戏场景时,我得到一个空引用。
尝试向相机 m_Targets 变换数组动态添加变换时出错。
有调试
Debug.Log("CC.m_Targets.Length" + CC.m_Targets.Length); //Troubleshooting
CC.m_Targets = new Transform[players.Length]; // array of size 1-4
NullReferenceException: Object reference not set to an instance of an object
GameSetupController.CreatePlayer () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:33)
GameSetupController.Start () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:14)
没有调试
CC.m_Targets = new Transform[players.Length]; // array of size 1-4
NullReferenceException: Object reference not set to an instance of an object
GameSetupController.CreatePlayer () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:34)
GameSetupController.Start () (at Assets/InfoGamerPhoton/Scripts/GameSetupController.cs:14)
CameraControl.cs
public Transform[] m_Targets; // All the targets the camera needs to encompass. [HideInInspector]
GameSetupController.cs
using Photon.Pun;
using System.IO;
using UnityEngine;
public class GameSetupController : MonoBehaviour
{
private CameraControl CC;
public GameObject[] players;
// This script will be added to any multiplayer scene
void Start()
{
CC = GetComponent<CameraControl>();
CreatePlayer(); //Create a networked player object for each player that loads into the multiplayer scenes.
}
private void CreatePlayer()
{
Debug.Log("Creating Player");
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), Vector3.zero, Quaternion.identity);
players = GameObject.FindGameObjectsWithTag("Player");
if (players.Length == 0)
{
return;
}
for (int i = 0; i < players.Length; i++)
{
Debug.Log("players.Length" + players.Length); //Troubleshooting
Debug.Log("CC.m_Targets.Length" + CC.m_Targets.Length); //Troubleshooting
CC.m_Targets = new Transform[players.Length]; // array of size 1-4
Debug.Log(CC.m_Targets.Length);
Debug.Log(players.Length);
CC.m_Targets[i] = players[i].transform;
Debug.Log("m_Targets : " + CC.m_Targets[i]);
Debug.Log("players : " + players[i]);
}
}
}
我的临时解决方案 - 在 CameraControl.cs
内private void FixedUpdate()
{
m_Targets = new List<Transform>();
//ADDS PLAYERS TO THE M_TARGETS LIST!
players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject child in players)
{
//Debug.Log(child.gameObject.transform.GetChild(0));
m_Targets.Add(child.gameObject.transform.GetChild(0));
}
您的 CC 对象为空,这就是空引用的来源。 GetComponent
只会找到与调用它的脚本附加到同一游戏对象的组件。如果对象是在编辑器中静态添加到场景中的,则可以使用 [SerializeField]
属性使私有对象在检查器中可见并手动分配引用。 Find
方法通常很昂贵,因此如果您可以通过将引用存储在公共位置或预先设置它们来避免使用它们,那么这是推荐的方法。