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
1
Question by Ochreous · Dec 16, 2012 at 05:47 PM · javascript

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++)
Comment
Add comment
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

1 Reply

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

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.

Comment
Add comment · Show 8 · 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 Ochreous · Dec 16, 2012 at 06:05 PM 0
Share

Why not? can it cause problems?

avatar image Bunny83 · Dec 16, 2012 at 06:11 PM 0
Share

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.

avatar image Ochreous · Dec 16, 2012 at 06:17 PM 0
Share

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.

avatar image Eric5h5 · Dec 16, 2012 at 06:34 PM 0
Share

@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.

avatar image Eric5h5 · Dec 16, 2012 at 07:10 PM 1
Share

@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.

Show more comments

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

13 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

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


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