- Home /
FindGameObjectsWithTag not working
I'm trying to implement a system that takes the online/offline characters, and then changes their audio clip to something different in the level they should. I've been having some problems with FindGameObjectsWithTag only on the online part of the script. Here's the relevant portion of the code:
if (Application.loadedLevelName != "Main Menu")
{
if (Application.loadedLevelName == levelToPlay)
{
levelToPlay = null;
if (Network.isServer || Network.isClient)
playingOnline = true;
players = GameObject.FindGameObjectsWithTag ("OnlinePlayer");problematic code
Debug.Log (players.Length); //this always returns 0.
if (players.Length == 0)
players = GameObject.FindGameObjectsWithTag ("OfflinePlayer");
for (int i = 0; i < players.Length; i++)
{
if (players[i] == null) break;
else
players[i].audio.clip = (AudioClip)Resources.Load (nextTrack, typeof(AudioClip));
}
}
GameObject.FindGameObjectsWithTag () only finds active game objects.
Answer by fifthknotch · Mar 04, 2014 at 03:51 AM
Make sure you have your variable "players" set up as an array. By declaring the variable as an array, your script worked fine for me.
I set up my players variable as an array. here's the declaration:
public GameObject[] players;
And if you want it, here's the entire script:
using UnityEngine;
using System.Collections;
public class VariableStorage : $$anonymous$$onoBehaviour
{
public GameObject playerPrefab;
public GameObject[] players;
private AudioClip Track;
public bool playingOnline = true;
public bool dreamRecall = false;
public bool Lucid = false;
public int movie = 1;
public int numberOfPlayers = 1;
public string levelDestination;
public string nextTrack;
public string levelToPlay;
public float $$anonymous$$Volume;
void Awake()
{
DontDestroyOnLoad (gameObject);
if (GameObject.Find ("ConsistantStorage(loaded)") && gameObject.name != "ConsistantStorage(loaded)")
Destroy (gameObject);
}
void Update()
{
if (Application.loadedLevelName != "$$anonymous$$ain $$anonymous$$enu") //in other words, if it loaded any level at all besides $$anonymous$$ain $$anonymous$$enu
{
if (Application.loadedLevelName == levelToPlay)
{
levelToPlay = null;
if (Network.isServer || Network.isClient)
playingOnline = true;
players = GameObject.FindGameObjectsWithTag ("OnlinePlayer"); //add a check to see if they equal to the total amount of players, in case one didn't load yet
Debug.Log (players.Length);
if (players.Length == 0)
players = GameObject.FindGameObjectsWithTag ("OfflinePlayer");
for (int i = 0; i < players.Length; i++)
{
if (players[i] == null) break;
else
players[i].audio.clip = (AudioClip)Resources.Load (nextTrack, typeof(AudioClip));
}
}
DontDestroyOnLoad (gameObject);
gameObject.name = "ConsistantStorage(loaded)";
}
if (playingOnline == true && Application.loadedLevelName != "$$anonymous$$ain $$anonymous$$enu") //basically if it started an online game yet or not
{
playingOnline = false;
Network.Instantiate(playerPrefab, GameObject.Find("trusty").transform.position, Quaternion.identity, 0);
}
}
}
When not connected, are your players tagged "OfflinePlayer"? If by default they are tagged "OnlinePlayer", then the script will search for the tag when you become a server/client and it will work. If you are offline, the script searches for "OfflinePlayer" as the tag. It will return 0 only if no gameObjects with said tag are found.
Sorry for the incredibly delayed response, but I finally got this working. Apparently what my problem was is that I had instantiating the online player in the same script. Instantiating the offline player is in another script. So basically what I was doing is that I was checking to see how many online players there were before I even spawned them. A little bit of reprogram$$anonymous$$g fixed the problem.
Thanks for all of your help.
Your answer
Follow this Question
Related Questions
Simple Code Not Working 4 Answers
OnSerializenetworkView not doing anything?? 1 Answer
Animation not working 1 Answer
Script not working 0 Answers
Gun reload not working? 1 Answer