- Home /
UnityScript self.ExampleList.get_(self a) cannot be assigned to
Hi everyone, I have an error in my Unityscript script and I not sure what is wrong. it says self.ExampleList.get_(self a) cannot be assigned to. Any idea how to fix it?
#pragma strict
import System.Collections.Generic;
var ExampleList:List.<int> = new List.<int>();
public var a:int;
function OnGUI(){
for ( a = 0; a <ExampleList.Count; a++)
if (GUILayout.Button("+", GUILayout.Width(50), GUILayout.Height(30)))
{
//self.ExampleList.get_(self a) cannot be assigned to
ExampleList[a]++;
}
for (var a = 0; a <ExampleList.Count; a++)
//there is already a variable named a.
for (var a = 0; a <ExampleList.Count; a++)
Answer by Bunny83 · Dec 16, 2012 at 06:02 PM
I guess Unityscript can't properly handle the indexer property of the list class when it comes to the post increment operator. Try this instead:
#pragma strict
import System.Collections.Generic;
var ExampleList:List.<int> = new List.<int>();
function OnGUI()
{
for (var a = 0; a < ExampleList.Count; a++)
{
if (GUILayout.Button("+", GUILayout.Width(50), GUILayout.Height(30)))
{
ExampleList[a] = ExampleList[a] + 1;
}
}
}
Btw: always use local variables for a for loop. A for loop variable should never be a member variable of a class.
A for loop initialized the variable everytime it's executed. So it makes no sense to store it in the class. Class members should have a meaning for the purpose of the class. Class variables occupies memory with you don't need in this case.
I have encountered something odd. If I declare a in the for loop and than declare it again I get this error. It Says that there is already a local variable named a. Is this suppose to happen? I can't post the code in the comments so I will edit my post and post it there.
@DangerousBeans: that's how JS handles scope. If you do "var a = 0" in the first loop, leave out var and make the second be just "a = 0". Code in comments works the same way as code in answers, namely 4 spaces in front of a line formats it as code. You can either do that manually or use the code format button in an answer and cut&paste the result into a comment.
@clunk47: I would suggest running Bunny83's script. Put the script on an object, add a few items to the list in the inspector, then start the scene. Click on the buttons and see what happens to the list items in the inspector.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
I need help with a script(brauche hilfe mit einen Script) 0 Answers
Resticing variables to whole numbers 3 Answers