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 ironsand · Apr 22, 2014 at 07:35 AM · javascript

How to declare typed array in UnityScript

I have a code like this.

 public var tileButtonRects = new Array();
 function  ShowTileButtons(){
   for(var i in [0,1,2]){
     for(var j in [0,1,2]){
       tileButtonRects[i+j*3] = new Rect(15+(i*28), 100+(j*26), 10, 15);
       GUI.DrawTexture(tileButtonRects[i+j*3], tiles['1s'+ ((i+1)+(j*3)).ToString()]);
     }
   }
 }

And I get this error.

Assets/GameMaster.js(31,38): BCW0028: WARNING: Implicit downcast from 'Object' to 'UnityEngine.Rect'.

As far as I understand the error message says that I need to declare the Object as Rect. But I couldn't find out how to do it.

I've read this document, and tried var tileButtonRects = Rect[];, but it didn't work. Could you tell me how to declare a Array of Rect Object?

Edit

I get BCE0048: Type 'System.Type' does not support slicing. error, with this code.

 #pragma strict
 
 var tileButtonRects = Rect[9];
 
 function Start () {
 }
 function Update () {
 }
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 Fattie · Apr 22, 2014 at 09:03 AM 0
Share

If you're a beginner, learn to use List - there are 1000 examples on here.

avatar image Hoeloe · Apr 22, 2014 at 09:40 AM 0
Share

I recommend generic lists over Unity's own List class. You'll have the same issue, otherwise.

avatar image Fattie · Apr 22, 2014 at 09:57 AM 0
Share

Hoe, just to be clear you mean

 using System.Collections.Generic;

then for example

 private List<ParseUser> threeTags;

and then

 threeTags = new List<ParseUser>();

and so on. (I didn't even realise unity had their own "List" class!) Exactly as Hoe ses don't use it, use the one in the example here!

avatar image Hoeloe · Apr 22, 2014 at 10:05 AM 0
Share

Yes, I meant that kind of List. However, that's C# syntax. The OP is writing in UnityScript.

avatar image ironsand · Apr 22, 2014 at 10:10 AM 0
Share

I'm a beginner, I'm glad to follow your instruction. But I'm using UnityScript(Javascript in Unity). Can I use C# List from UnityScript?

Show more comments

1 Reply

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

Answer by Hoeloe · Apr 22, 2014 at 07:38 AM

When defining a typed array (which you should ALWAYS use over the Array class), you need to define the length of the array. Arrays are fixed-length constructs. One they're initialised, you cannot change their length (if you need to, then you should be using generic Lists).

Instead of using var tileButtonRects = new Rect[];, you need something along the lines of var tileButtonRects = new Rect[5];, for example, which would create an array of length 5.

EDIT: Have updated my answer. It looks like you need the new keyword in this case, from a quick bit of research.

Comment
Add comment · Show 7 · 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 ironsand · Apr 22, 2014 at 08:53 AM 0
Share

Thanks for your answer. I replaced as you wrote, then I got a error Assets/Game$$anonymous$$aster.js(4,23): BCE0048: Type 'System.Type' does not support slicing. Any idea how to fix this?

avatar image Hoeloe · Apr 22, 2014 at 09:41 AM 0
Share

Sounds like you're not setting up the array correctly. Post your full code.

avatar image Fattie · Apr 22, 2014 at 09:58 AM 0
Share

it's possible you have ( ) rather than [ ]

But just forget it and change to List as in the examples. 1000s of examples here about using List

here's the awesome doco to book mark

http://msdn.microsoft.com/en-us/library/d9hw1as6(v=vs.90).aspx

avatar image Hoeloe · Apr 22, 2014 at 10:02 AM 0
Share

List is not better than an array for fixed-length examples. Their performance is almost comparable, but they result in slightly messier code. Better to use an array if you can.

avatar image ironsand · Apr 22, 2014 at 11:36 AM 0
Share

Oh, I've written Rect[9]. I should have wrote new Rect[9]. There is still IndexOutOfRange error. But I think I can fix it myself. Thanks for your help!

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

22 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

Related Questions

semicolon expected while semicolon is present 1 Answer

Best way to copy Struct-classes in UnityScript? 1 Answer

Example @Serializable ? Please. 1 Answer

Highscoring and changing scripts (Java/UnityScript) 1 Answer

Updating UI Slider through another script 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