Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Benifir · Sep 10, 2013 at 09:43 AM · js-c#

Error with converted Function?

What is wrong with this function? The whole script is located Here if needed.

Error: Assets/Chat/Chat.cs(115,68): error CS1061: Type object' does not contain a definition for playerid' and no extension method playerid' of type object' could be found (are you missing a using directive or an assembly reference?)

     void  OnPlayerDisconnected ( NetworkPlayer disconnectedPlayer  ){
         var currentCount= 0;
         foreach(var entry in playerList) {
             if (entry.playerid == disconnectedPlayer) {
                 playerList.RemoveAt(currentCount);
                 networkView.RPC("SendRemovePlayerName", RPCMode.Others, entry.playname);
                 return;
             }
             currentCount++;
         }
     }
Comment
Add comment · Show 6
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 dorpeleg · Sep 10, 2013 at 10:35 AM 0
Share

Just change "for" to "foreach".

 foreach(var entry in playerList) {
avatar image Sajidfarooq · Sep 10, 2013 at 10:37 AM 0
Share

@Dorpe: You should convert your comment into an answer.

avatar image ArkaneX · Sep 10, 2013 at 12:05 PM 1
Share

I have just checked the linked script, and playerList is ArrayList. When you iterate it using

 foreach (var entry ....

entry will be of type object. The best way to handle this, is to change playerList type from

 private ArrayList playerList= new ArrayList();

to

 private List<serverPlayerEntry> = new List<serverPlayerEntry>();

This requires adding

 using System.Collections.Generic;

at the top of the script.

avatar image Hoeloe · Sep 10, 2013 at 01:17 PM 1
Share

I don't think this code will work. You can't edit a list during a foreach iteration through it (as this can cause some unexpected results). Ins$$anonymous$$d, what you should do is set a boolean (`remove = true`, or similar), and remove the item from the list after the foreach loop has ter$$anonymous$$ated. Alternatively, you could use a for loop, ins$$anonymous$$d of a foreach loop (that way, currentCount is dealt with for you, which is another bonus). I don't recommend using a for loop if playerList is a LinkedList, though, as you need to explicitly index each item in the list, and with LinkedList, that becomes an O(n^2) operation, as opposed to the O(n) operation on a regular List. If you do use a for loop, keep in $$anonymous$$d that removing an element will change the index of the elements after it (hence why you can't do it during a foreach loop), so look out for that.

avatar image Benifir · Sep 12, 2013 at 09:41 AM 0
Share

@Hoeloe: I just changed that line to a for loop ins$$anonymous$$d of a foreach and it works like a charm. The scripts working as it should now so thanks to everyone that gave input.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

How to call/Execute from C# to JS? 1 Answer

Social Network JS Sdk 0 Answers

How do you translate transform js>c# 1 Answer

Create a guitexture When clicked on 3d text? 0 Answers

C# Camera.main.ScreenPointToRay on android 1 Answer


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