I am trying to get all of the game objects with the tag of player but it comes up with the error "Cannot implicitly convert type int to UnityEngine.GameObject[]". My code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
public class mymanager : MonoBehaviour
{
GameObject[] amount;
public GameObject winPanel;
void Start()
{
winPanel.SetActive(false);
}
// Update is called once per frame
void Update()
{
amount = GameObject.FindGameObjectsWithTag("Player").Length;
if(amount <= 1)
{
winPanel.SetActive(true);
}
else
{
winPanel.SetActive(false);
}
}
public void OnClickJoinLobby()
{
PhotonNetwork.LoadLevel("Lobby");
}
}
I wanted to count the amount of players and open a UI panel if there was a maximum of 1 player left.
CodePudding user response:
You gave the array of Gameobject amount
an int value which is wrong, you need to give it Gameobjects, delete .length
amount = GameObject.FindGameObjectsWithTag("Player");
And in the if condition check the length of it like this
if(amount.Length <= 1)
{
///
}
CodePudding user response:
GameObject.FindGameObjectsWithTag("Player").Length
returns the length of the array returned by GameObject.FindGameObjectsWithTag("Player")
.
If you only want how many player exists, you don't need the GameObject[] amount
, just check the length:
int amount = GameObject.FindGameObjectsWithTag("Player").Length;
if (amount <= 1) ...
Or if you want to do with them something, then
private GameObject[] _players;
...
_players = GameObject.FindGameObjectsWithTag("Player");
int amount = _players.Length;
...