• 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 ZiggySawdust · Nov 11, 2014 at 02:25 PM · arrays

Array Element Issues

So for the game i am making i require an array to hold 50 string elements. What i want to happen is when the player pushes a button the script randomly chooses a number and finds the corrosponding element. I then want the script to then show the selected element in some sort of GUI output such as a label. I get it to work but after a random number of presses i get an error saying it cannot cast from source type to destination type. I was thinking of changing the script to C# and use lists but the sake of continuity i was just seeing if what i could do was possible.

 function OnGUI() {
   GUI.Label(Rect(50,50,50,50),ChosenSong,"label");
   if (GUI.Button(Rect(Screen.width/2,Screen.height/2,150,50),Toggle)){
     Song = Random.Range(0,playlist.length);
     ChosenSong = playlist[Song];
     playlist.RemoveAt(Song);
   }
 }
 


Comment
Add comment · Show 3
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 ZiggySawdust · Nov 11, 2014 at 01:45 PM 0
Share

In the start function i add all the elements but to save on character space i left them out.

InvalidCastException: Cannot cast from source type to destination type. songlist80s.OnGUI () (at Assets/songlist80s.js:73)

var playlist = new Array (String); var Song:int; var ChosenSong:String; var Toggle:String; function Start (){

 }
 
 function Update () {
 if (playlist.length>0){
 Toggle= "Choose A Song";
 }else{
 Toggle="Completed";
 }
 }
 
 function OnGUI() {
 GUI.Label(Rect(50,50,50,50),ChosenSong,"label");
 if (GUI.Button(Rect(Screen.width/2,Screen.height/2,150,50),Toggle)){
 Song = Random.Range(0,playlist.length);
 ChosenSong = playlist[Song];
 playlist.RemoveAt(Song);
 }
 }

avatar image ZiggySawdust · Nov 11, 2014 at 02:56 PM 0
Share

line 15 sorry

avatar image Jeff-Kesselman · Nov 11, 2014 at 06:11 PM 0
Share

I down voted your question because you down voted the right answer.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Nov 12, 2014 at 05:16 AM

Never use the JS Array class. Use generic lists instead, and make sure the type of the variable matches the type of the list.

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
-2

Answer by Jeff-Kesselman · Nov 11, 2014 at 02:25 PM

The maximum index of an array is array.Length-1, not array.Length

Comment
Add comment · Show 5 · 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 Jeff-Kesselman · Nov 11, 2014 at 06:06 PM 0
Share

I have no idea why you down voted this. It is the correct answer.

Fix this line:

Song = Random.Range(0,playlist.length);

avatar image MrSoad · Nov 11, 2014 at 06:18 PM 0
Share

I don't think he could have down voted you, he has to have 100 karma to do that and he has no down votes on his page.

Voted you both back up :)

PS: Not Landern either so don't blame him :D

avatar image Jeff-Kesselman · Nov 11, 2014 at 06:20 PM 0
Share

Fair enough :)

avatar image ZiggySawdust · Nov 12, 2014 at 04:52 AM 0
Share

yea i didnt vote you down and i made your correction thank you for pointing tht out but i still get the same error

avatar image Eric5h5 · Nov 12, 2014 at 05:12 AM 0
Share

I just downvoted this answer because it's incorrect. Random.Range used with integers is exclusive on the upper bound; see the docs. Random.Range (0, array.Length) is correct. If you use array.Length-1, it will never choose the last item in the array.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to select a texture in an inventory? 0 Answers

How to load an inventory array? 1 Answer

need to shorten my code but unsure of how 3 Answers

Change multiple gameobjects colour to create a trail 0 Answers

2D Platformer (Unity 4.3) - How are the AudioClip arrays initialized? 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