Respawn with arrays
I have a Script (GameManager) when after starting the game he selects a random Prefab player from a string
GameManager----
[SerializeField] private Transform[] _spawns;
public string[] CarsPlayerList;
Public void CreatePlayer(){
PhotonNetwork.Instantiate(CarsPlayerList[UnityEngine.Random.Range(0, CarsPlayerList.Length)], _spawns[Random.Range(0, _spawns.Length)].position, Quaternion.identity);
}
My second Script (CarController) has a void called Kill, the goal is that when I press the BackSpace button, the player is destroyed (by photonNetwork.destroy) and CarController calls GameManager CreatePlayer, but when it calls void to instantiate, the message appears that a new prefab cannot be instantiated because it is null
CarController
void Kill()
{
if (photonView.IsMine)
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
PhotonNetwork.Destroy(gameObject);
gm.GetComponent<GameManager>().CreatePlayer();
}
}
}
Unity Mensage
ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.Instantiate[T] (T original) (at <cfc1ad890650411e946cff2e6f276711>:0)
GameManager.CreatePlayer() (at Assets/Script/GameManager.cs:83)
GameManager.AddPlayer() (at Assets/Script/GameManager.cs:58)
the AddPlayer() that unity mentions is this one
[PunRPC]
public void AddPlayer()
{
PlayerInGame++;
if (PlayerInGame== PhotonNetwork.PlayerList.Length)
{
CreatePlayer();
}
}
I suppose it's a logic error I created because as it gets instantiated via a string it gives an error with player.length of photonNetwork... but how does it respawn via a string after it dies then?
GameManager----
[SerializeField] private Transform[] _spawns;
public string[] CarsPlayerList;
Public void CreatePlayer(){
PhotonNetwork.Instantiate(CarsPlayerList[UnityEngine.Random.Range(0, CarsPlayerList.Length)], _spawns[Random.Range(0, _spawns.Length)].position, Quaternion.identity);
}
My second Script (CarController) has a void called Kill, the goal is that when I press the BackSpace button, the player is destroyed (by photonNetwork.destroy) and CarController calls GameManager CreatePlayer, but when it calls void to instantiate, the message appears that a new prefab cannot be instantiated because it is null
CarController
void Kill()
{
if (photonView.IsMine)
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
PhotonNetwork.Destroy(gameObject);
gm.GetComponent<GameManager>().CreatePlayer();
}
}
}
Unity Mensage
ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.Instantiate[T] (T original) (at <cfc1ad890650411e946cff2e6f276711>:0)
GameManager.CreatePlayer() (at Assets/Script/GameManager.cs:83)
GameManager.AddPlayer() (at Assets/Script/GameManager.cs:58)
the AddPlayer() that unity mentions is this one
[PunRPC]
public void AddPlayer()
{
PlayerInGame++;
if (PlayerInGame== PhotonNetwork.PlayerList.Length)
{
CreatePlayer();
}
}
I suppose it's a logic error I created because as it gets instantiated via a string it gives an error with player.length of photonNetwork... but how does it respawn via a string after it dies then?
0
Answers
-
Closing this as duplicate of
https://forum.photonengine.com/discussion/18867/respawn-system-with-strings0
This discussion has been closed.