- Home /
Why is my list not adding?
My list isn't adding at all, even though all of the logs print out to the console.
Here's my code:
public void AddPlayerProfile(PlayerProfile pPro)
{
photonView.RPC("rpcPlayer", PhotonTargets.AllBuffered, pPro.getPlayerID());
}
[PunRPC]
public void rpcPlayer(int id)
{
PlayerProfile[] profiles = GameObject.FindObjectsOfType<PlayerProfile>();
print (profiles.Length.ToString() + " length");
print (id.ToString() + " player normal id");
for(int i = 0; i < profiles.Length; i++)
{
if(profiles[i].getPlayerID() == id)
{
print ("adding");
spawnedPlayers.Add(profiles[i]);
print (spawnedPlayers[0].GetPlayerName());
print ("id is equal");
break;
}
else
{
print ("id is not equal");
}
}
print ("we got this far");
globalRoom.UpdateProfiles();
}
Here's the console log:
And here's what the list looks like after that:
I'm not sure why it's not adding, and as you can see my other team lists are adding and I'm doing the exact same thing.
Hi, @Whiteleaf. Everything seems right so far. We'll need more information to diagnosis the issue. Please provide us the source code that tell us more about spawnPlayers
(like it's type and how it's declared) and it's add function ( spawnedPlayers.Add(profiles[i]);
). Thanks!
$$anonymous$$ight be a serialization problem, not all classes are supported (in your case it would be the profiles class) so I dont think you will be able to see them in the editor. Obviously it is added since you can retrieve spawnedPLayers[0].
Spawn players is initialized where it's made:
public List<PlayerProfile> spawnedPlayers = new List<PlayerProfile>()
Code might not be 100% accurate as I'm on my phone right now but it is just assigned where it's made.
I'm almost positive it is not a serialization issue, as my 2 other lists which handle $$anonymous$$m 1 and $$anonymous$$m 2 players function the exact same way except they just add to the corresponding list depending on their $$anonymous$$mID, and i can see them in the inspector.
Everyone, I've narrowed it down to my UpdateProfiles() function. This for some odd reason is making it not add the player. One mistake I made was not calling and RPC function with the photonView, but that didn't fix it. Here's the UpdateProfiles() function:
[PunRPC]
public void UpdateProfiles()
{
print ("called update profiles");
List<PlayerProfile> visualPlayers = database.spawnedPlayers;
for(int i = 0; i < visualPlayers.ToArray().Length; i++)
{
visualPlayers.RemoveAt(i);
}
for(int i = 0; i < database.spawnedPlayers.ToArray().Length; i++)
{
visualPlayers.Add(database.spawnedPlayers[i]);
}
print("visual players " + visualPlayers.ToArray().Length.ToString());
foreach(PlayerProfile p in visualPlayers.ToArray())
{
PlayerGlobalProfile profile = new PlayerGlobalProfile();
profile.playerName = p.GetPlayerName();
profile.total$$anonymous$$ills = 0;
profile.totalDeaths = 0;
profile.totalScore = 0;
profile.id = p.getPlayerID();
globalProfiles.Add(profile);
}
}
Answer by EmHuynh · Feb 16, 2016 at 03:36 AM
Hello, @Whiteleaf! Let's check out your UpdateProfiles() function:
public void UpdateProfiles()
{
print( "UpdateProfiles() called.")
List< PlayerProfile > visualPlayers = database.spawnedPlayers;
for( int i = 0; i < visualPlayers.ToArray().Length; i ++ ) {
visualPlayers.RemoveAt( i );
}
for( int i = 0; i < visualPlayers.ToArray().Length; i ++ ) {
visualPlayers.Add( database.spawnedPlayers[ i ] );
}
...
}
First, let's begin with this line: List< PlayerProfile > visualPlayers = database.spawnPlayers;
visualPlayers is declared to be a reference of database.spawnPlayers
(think pointer). Any changes you apply to visualPlayers
will occur to database.spawnedPlayers
. Click this link to see a good example of that case..
If you wanted to copy the contents of database.spawnedPlayers
and not be a reference of it, here's how visualPlayers should have been initialized: visualPlayers = new List< PlayerProfile >( spawnedPlayers );
.
Next, the for loops in the function are pointless. The condition of the first and second for loop are identical ( int i = 0; i < visualPlayers.ToArray().Length; i ++
) . The first one loops through visualPlayers
and remove each of it's element. The second loop tries to do the opposite. One way or the other, these loops made the initialization of visualPlayers
useless.
Finally, the solution is to remove the for loops & visualPlayers
and change the condition of the foreach statement. Here's the simple and updated version of the function:
public void UpdateProfiles()
{
foreach( PlayerProfile p in database.spawnedPlayers.ToArray() )
{
PlayerGlobalProfile profile = new PlayerGlobalProfile();
profile.playerName = p.GetPlayerName();
profile.totalKills = 0;
profile.totalDeaths = 0;
profile.totalScore = 0;
profile.id = p.getPlayerID();
globalProfiles.Add(profile);
}
}
This problem should be resolved. If you have any more questions, let us know! (:
The loops there are simply to remove any excess players. All the function does is to update the scoreboard each time a player joins. If I do this normally, it adds the current player and the other players, so basically everyone that was in the game would be added twice. This is why I made the extra list, which I was going to cycle through, remove all of the instances, and then add them back depending on how many spawned players there were. I would then grab their values, such as kills, deaths, score, etc.
Thank you for your help. :)
@WhiteLeaf, that explains a lot, thanks for the clarification! I am glad I could help (:
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Copy values between two classes in two lists. 1 Answer
Make Lists within a List 0 Answers
Help: items not getting added to list. 1 Answer
Unable to remove GameObjects from list 4 Answers