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 tlear1 · Jan 11, 2016 at 04:15 AM · scripting problem

Javascript - Generic List question

Hello, first time writer, long time reader.

Javascript code.

I am trying to construct a inventory system. I was following along some guides and came to a point where I was stuck. The video was using C#, so I was attempting to convert from C# to JS as I went along which worked until I came to the below issue.

I have a ItemDatabase that uses a generic List "Items" that I can edit in the inspector.

I'm trying to pull the variables from that list to another script. I can't seem to figure this out and searching generic lists in google gives a ton of answer that are not helpful here.

this is my list in script Database, it's pulling the variables from script "Items".

var items : List. = new List.();

This is a sample of the variables in the script "Items";

public var itemID = 0; public var itemAttack = 0;

I then go and set the values for each item in the inspector. This allows me to easily grow the list as I go along.

Now say i want to grab the itemAttack value for calculating attack, how could I grab that?

If I use items[1].itemAttack (the item I'm trying to use being the second item in the list) in the script Database it pulls just fine, I've tested with Debug.Log. However if I try to utilize that in another script it does not. I usually get Object does not support slicing.

I was thinking that in the script Database it knows it's a list and therefore can read the [] and know what I am asking. However if I try to pull it in another script I figured the script didn't know there were variables stored in the list so it wasn't pulling them.

Do I need to declare something in every script I intend to have the values set in?

I tried to set a var ItemList to GameObject.Find("ItemDatabase").GetComponent(Database).items;

but that seems to set that var to a Object rather then the list.

Any help would be appreciated. I've spent most of my weekend reading and I'm not finding a solution.

Thank you,

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 commodore · Jan 11, 2016 at 01:53 PM 0
Share

You could turn your list into an array and access it that way

 items.ToArray()[1]

0 Replies

· Add your reply
  • Sort: 

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

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

What part of my camera script is increasing my cameras X rotation? 1 Answer

how do I rezolve "look rotation viewing vector is zero"? 1 Answer

Moving Platform 3D 1 Answer

Raycasting Animations 0 Answers

Button problem (noob) 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