Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Whiteleaf · Feb 15, 2016 at 04:28 AM · listlistspun

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:

alt text

And here's what the list looks like after that:

alt text

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.

why.png (12.7 kB)
atabase.png (6.7 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EmHuynh · Feb 15, 2016 at 06:58 AM 0
Share

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!

avatar image Salmjak · Feb 15, 2016 at 11:26 AM 0
Share

$$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].

avatar image Imankit · Feb 15, 2016 at 01:35 PM 0
Share

Where are you initializing spawnPlayers?

avatar image Whiteleaf · Feb 15, 2016 at 03:56 PM 0
Share

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.

avatar image Whiteleaf · Feb 16, 2016 at 12:29 AM 0
Share

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);
         }
     }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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! (:

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Whiteleaf · Feb 16, 2016 at 04:25 AM 1
Share

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. :)

avatar image EmHuynh Whiteleaf · Feb 16, 2016 at 04:57 AM 0
Share

@WhiteLeaf, that explains a lot, thanks for the clarification! I am glad I could help (:

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges