6 Replies - 541 Views - Last Post: 31 March 2013 - 10:01 AM Rate Topic: -----

#1 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

TextAsset inside the Array - UnityScript/JavaScript

Posted 30 March 2013 - 11:12 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: TextAsset inside the Array - UnityScript/JavaScript

#2 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: TextAsset inside the Array - UnityScript/JavaScript

Posted 30 March 2013 - 12:53 PM

i have made some modifications
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 = Mathf.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);
    }
}


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

This post has been edited by ButchDean: 31 March 2013 - 09:56 AM
Reason for edit:: Added code tags! >;

Was This Post Helpful? 0
  • +
  • -

#3 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: TextAsset inside the Array - UnityScript/JavaScript

Posted 30 March 2013 - 01:03 PM

my error message

The best overload for the method 'System.Collections.Generic.List.<UnityEngine.TextAsset>.Add(UnityEngine.TextAsset)' is not compatible with the argument list '(int)'.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,438
  • Joined: 12-December 12

Re: TextAsset inside the Array - UnityScript/JavaScript

Posted 30 March 2013 - 01:31 PM

View PostTrewSx, on 30 March 2013 - 08:03 PM, said:

my error message

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

That error message seems quite clear: you have created a List of TextAssets but you are attempting to add temp to this list, which is just an integer value.

BTW You instantiate your list with new twice.
Was This Post Helpful? 0
  • +
  • -

#5 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: TextAsset inside the Array - UnityScript/JavaScript

Posted 30 March 2013 - 01:41 PM

Trying to use an integer seems to be the problem, but how do i get a random number of items from my TextAsset ?


I have 10 items there Axe +1, +2 etc
Was This Post Helpful? 0
  • +
  • -

#6 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: TextAsset inside the Array - UnityScript/JavaScript

Posted 30 March 2013 - 01:51 PM

i also changed my var temp

var temp : int = Random.Range(0,10) + weaponsList.Count;

so this will give me random values between 0 and 10 and not only 0 as before

Does not fix anything tho
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: TextAsset inside the Array - UnityScript/JavaScript

Posted 31 March 2013 - 10:01 AM

You're not being very much help to yourself nor anyone else here, TBH. How have you corrected:

Quote

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

?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1