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 Odd Sock · Apr 02, 2014 at 09:52 PM · javascriptarraysprite

How to get an array of sprite arrays to work (javascript)?

I'm trying to make an array of sprite arrays (using javascript) This script handles the sprites of the players

   var Classes : Sprite[,];//this doesn't work
         
     var classSpearmanSprite : Sprite[];
     var classKnightSprite : Sprite[];
     var classFarmhandSprite : Sprite[];
     
         function Start()
         {
         
         Classes = [classSpearmanSprite, classKnightSprite, classFarmhandSprite];
         }
         function CharacterSprites ()
         {
             var allChars : GameObject[];
             allChars = GameObject.FindGameObjectsWithTag("Player");//put all players in an array
             
             for (var i = 0; i < allChars.length; i++) //for each player in the array
             {
                 var classNum = allChars[i].GetComponent(CharStats).charClass; //get class number from the player
                 var neededSprite : int = allChars[i].GetComponent(PlayerMove).playerDirection;//returns either 1,2,3,4
         
                 
         // the next three lines i'd prefer just to have a single line:
 //Classes[classNum,neededSprite], so which ever class a player is it is all done through a single line
 //so i don't have to add more lines for every class in the future
                 if (classNum == 0) allChars[i].GetComponent(SpriteRenderer).sprite = classSpearmanSprite[neededSprite];
                 if (classNum == 1) allChars[i].GetComponent(SpriteRenderer).sprite = classKnightSprite[neededSprite];
                 if (classNum == 2) allChars[i].GetComponent(SpriteRenderer).sprite = classFarmhandSprite[neededSprite];
                 //for each player, get the sprite of the renderer, make it the sprite of the player's class and the character's direction, direction is an int: 1,2,3,4
                 
             }
         }

thanks in advance

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

2 Replies

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

Answer by Odd Sock · Apr 07, 2014 at 03:46 PM

     static var Classes : Sprite[,] = new Sprite[3,9];
     function Start()
     {
     for (var j : int =0; j < 9; j++)
         {
             Classes[0,j] = classSpearmanSprite[j];
             Classes[1,j] = classKnightSprite[j];
             Classes[2,j] = classFarmhandSprite[j];
         }
     }
     
     function CharSprites()
     {
     allChars[i].GetComponent(SpriteRenderer).sprite = Classes[classNum,neededSprite];
     }

this works for me, but thanks for the effort fafase

Comment
Add comment · 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
0

Answer by fafase · Apr 03, 2014 at 06:11 AM

Try to use jagged arrays:

 var Classes : Sprite[][] = new Sprite[3][];
 
 var classSpearmanSprite : Sprite[];
 var classKnightSprite : Sprite[];
 var classFarmhandSprite : Sprite[];

 function Start(){
     Classes[0] = classSpearmanSprite;
     Classes[1] = classKnightSprite;
     Classes[2] = classFarmhandSprite;
 }

then you use those like:

 void Method(){
    // Get the first spearman sprite
    Sprite sp = Classes[0][0];
 }
Comment
Add comment · Show 6 · 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 Odd Sock · Apr 03, 2014 at 11:58 AM 0
Share

Whenever I tried to do: var Classes : Sprite[][] = new Sprite[3][]; it gives me an error (insert semicolon)

avatar image fafase · Apr 03, 2014 at 12:52 PM 0
Share

According to this Js does not take the type:

  var Classes = new Sprite[3][];

You could also use a Dictionary:

 import System.Collections.Generic;
 enum SpriteType{Spearman,$$anonymous$$night,Farmhand}
 var dict = new Dictionary.<SpriteType, Sprite[]>();
 function Start(){
    dict.Add(SpriteType.Spearman,classSpearmanSprite);
    dict.Add(SpriteType.$$anonymous$$night,class$$anonymous$$nightSprite);
    dict.Add(SpriteType.Farmhand,classFarmhandSprite);
 }
 function GetSpriteArray(type:SpriteType)
 {
      return dict[type]; 
 }

Good point with this principle, you can only use sprite type that are declared in the enum.

avatar image Odd Sock · Apr 03, 2014 at 06:08 PM 0
Share

thanks, yet I'm unsure how to set the sprite of a player using classNum and neededSprite.

avatar image fafase · Apr 03, 2014 at 06:16 PM 0
Share

The magic of enum is that they also are integers. The first is 0, the second is 1 and so on.

avatar image Odd Sock · Apr 03, 2014 at 06:32 PM 0
Share

i've got this so far: allChars[i].GetComponent(SpriteRenderer).sprite = dict[whatgoeshere?]; but how do I get the sprite out of it?

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

20 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

Related Questions

Physics.OverlapSphere with a min radius? 1 Answer

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 2 Answers

Javascript - find a value in an array 0 Answers

Array Index out of range (JavaScript) 1 Answer

Get Sprite index in array by filename 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