Cannot convert 'String' to 'Array'.
Hello, so here is my code:
#pragma strict
var _item : Item = GetComponent(Item);
var itemSlot = new Array[19];
function Start ()
{
for(var i : int = 0;i >= itemSlot.length;i++)
{
itemSlot[i] = new _item.Item("empty");
}
}
And the Item.class is:
#pragma strict
function Item (name : String)
{
return name;
}
While executing this code I keep getting an error which says: .../ItemS.js(8,33): BCE0022: Cannot convert 'String' to 'Array'.
I searched for the answer, but no luck, obviously I`m creating the array in a wrong way or something, but I cant make it work, so please help. :)
im not big on javascript, but that part where you do _item.Item("empty"); is definately whats giving you the error.
for one thing, having a class called Item and a function called Item is a horrible idea, im surprised it even let you compile that. change the name of that function to something else.
Also, in your loop youre checking if i is bigger than or equal to the array length, basically this means that youre going to be constantly looping through that since i++ will never be called.
fix those then see if the error changes at all
Answer by Positive7 · Sep 04, 2015 at 08:12 PM
#pragma strict
var _item : Item; //GetComponent should be Initialized in Start() or Awake()
var itemSlot : String[] = new String[19]; //You missed the "Array" here
function Start ()
{
_item = GetComponent(Item);
for (var i = 0; i < itemSlot.length; i++) { //i is already int dosen't need it. ">" should be "<"
itemSlot[i] = new _item.Item("empty");
Debug.Log(itemSlot[i]); //Only for Debug to see if we succed, because Array can't be seen in Inspector.
//Or you could change Array to String eg.:
//" var itemSlot : String[] = new String[19]; "
}
}
Thank you for help guys! Still getting errors though. If using your code @Positive7 I`m getting: "Object reference not set to an instance of an object" on line 9. (itemSlot[i] = new _item...)" If changing string back to array - the previous error occurs (cant convert array to string). I could sort something out but the problem is the Item object was only for testing purposes, and it should later have more variables in it like which may differ in type for example: function Item(name : String, size : int and so on) so that is way I cant use String, I`m pretty sure in Java you can use Object as an variable itself, like: "var ItemSlot : Item[] = new Item[19];" But I cant find a way to do it in Unity script.
Ill think of other way of doing this then, maybe I
ll sort something out. Thank you for your help guys. :)
I'm not sure if it's possible to store to different value in an Array like string and int. I would do something like this using List:
Item.js
#pragma strict
class Item{
public var id : int;
public var name : String;
}
Inventory.js
#pragma strict
import System.Collections.Generic;
var listSize : int = 19;
var list : List.<Item> = new List.<Item>();
function Start ()
{
for (var i = 0; i < listSize; i++) {
list.Add(new Item());
for (var k = 0; k < list.Count; k++) {
list[k].id = k;
list[k].name = "Empty"+k;
}
}
}
Answer by Henris088 · Sep 04, 2015 at 06:49 AM
Thanks bro. I will try these. EDIT: Yeah I have tried these mate - same thing, put it back together because I`m pretty sure its correct, no disrespect, thanks for trying to help, but obviously the problem is with arrays, I cant sort it out anyhow. :||
i think the other problem might be how yourre creating youre array. try doing var itemSlot : String[];
then in the Start Function, do itemSlot = new String[19];
Also, (not 100% sure about this part) but youre declaring a variable at the top, then attempting to create a new one 19 times. in addition to what i suggested before, try getting rid of the _item variable altogether, then doing itemslot[i] = new Item.Item("empty");