Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Henris088 · Sep 04, 2015 at 04:47 AM · arrayfunctionarraysstringstrings

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. :)

Comment
Add comment · Show 1
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 Vice_Versa · Sep 04, 2015 at 05:49 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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]; "
      }
  }
Comment
Add comment · Show 2 · 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 Henris088 · Sep 06, 2015 at 02:21 AM 0
Share

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 Ill sort something out. Thank you for your help guys. :)

avatar image Positive7 · Sep 06, 2015 at 12:00 PM 0
Share

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;
            }
         }
         
   }

avatar image
0

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. :||

Comment
Add comment · Show 1 · 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 Vice_Versa · Sep 04, 2015 at 07:37 PM 0
Share

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");

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove empty "" strings from array String[] 1 Answer

Is it possible to have 2 references in an array? 1 Answer

Pull int from string if within square brackets 1 Answer

Storing functions to use later 0 Answers

Wher is the problem? My string isn't behaving... 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