- Home /
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++;
}
}
Just change "for" to "foreach".
foreach(var entry in playerList) {
@Dorpe: You should convert your comment into an answer.
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.
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.
@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.
Your answer
Follow this Question
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