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 TrewSx · Mar 30, 2013 at 05:42 PM · javascriptarraytextasset

TextAsset inside the Array - UnityScript/JavaScript

Hello i have a question regarding Arrays. Let's say i have a CSV file called weapons, i changed that CSV file into a TextAsset file and i would like to have an Array which will hold all my weapons from this TextAsset file and then in my function i will iterate through this Array to create a random number of items.

 var weapons        :   TextAsset;
 var itemsListArray :   Array;
 
 function  InitialiseArray() {
     
     itemsListArray = new Array ();
     
     CreateRandomItemsFromArray();
 }
 
 private function CreateRandomItemsFromArray() {
     
   var total : int = Mathf.Clamp(Random.value * 10, 4, 10);
     
     for(var _cnt : int = 0; _cnt < total; _cnt++) {
         
             var    temp : int = Random.value * 24;
             itemsListArray.Add(temp);
             //Debug.Log(_cnt + " -- " + itemsListArray);
     }
 }

When i am passing weapons as a parameter in itemsListArray = new Array (); i am getting this error, but the items are being printed in Console.

InvalidCastException: Cannot cast from source type to destination type. Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value)

Also inside the window which is being opened to create random set of items. My first GUI Button is empty ad the rest of them are having integer values from CreateRandomItemsFromArray function

Comment
Add comment · Show 4
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 TrewSx · Mar 30, 2013 at 07:51 PM 0
Share

I changed my code and it's all working except i dont know how to store my newly created Random range of my items.

 import System.Collections.Generic;
 
 public var weaponsList : List.<TextAsset> = new List.<TextAsset>();         
 
 function  InitialiseArray() {
  
     weaponsList = new List.<TextAsset>();
  
     CreateRandomItemsFromArray();
 }                
 
 private function CreateRandomItemsFromArray() {
 
     var total : int = $$anonymous$$athf.Clamp(Random.value * 10, 4, 10);
 
        for(var _cnt : int = 0; _cnt < total; _cnt++) {
 
      var temp : int = Random.Range(0,weaponsList.Count);

         //weaponsList.Add(temp); //problem is here

         Debug.Log(_cnt + " -- " + weaponsList);
     }
 }
avatar image TrewSx · Mar 30, 2013 at 08:02 PM 0
Share

my error message

The best overload for the method 'System.Collections.Generic.List..Add(UnityEngine.TextAsset)' is not compatible with the argument list '(int)'.

avatar image Eric5h5 · Mar 30, 2013 at 08:05 PM 0
Share

A TextAsset list can only hold TextAssets, not ints.

avatar image TrewSx · Mar 30, 2013 at 08:23 PM 0
Share

so how do i get a random item from my weapon TextAsset ?

1 Reply

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

Answer by Eric5h5 · Mar 30, 2013 at 06:16 PM

Never use the Array class. Use a generic List, then you won't have those casting issues, plus it's much faster and better. Also you can just use Random.Range instead of using Random.value like that.

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

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

11 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

Related Questions

Creating a dynamic array of objects of custom class 1 Answer

How to Create Dynamic Multidimensional Array? 1 Answer

Array - insert item in the middle 1 Answer

Error creating 5-dimensional array 1 Answer

Multidimensional array variable size (JS) 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