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 · Apr 06, 2013 at 07:00 PM · gameobjectarray

Array of GameObjects - JS

Hi, i have a question here about Arrays.

I have two variables

 var itemsSlots :  GameObject[];
 
 var itemsListArray  :   Array;

there are three prefab items in itemsSlot which i added in the Inspector.

When i am calling this function

 function SomeFunction() {
     
     for(var i = 0; i < itemsSlots.Length; i++) {
         print("for loop       = " + itemsSlots[i]);        
         itemsListArray.Add(i);
      
     }    
 }

what exactly am i storing in itemsListArray.Add(i) ? Is it only number of items in itemsSlots in this case 3 ? Therefore i can refer to those objects only as to integers. And if yes how can i store and retrieve the actual object originally added in the Inspector.

Comment
Add comment
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

1 Reply

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

Answer by Hotshot10101 · Apr 06, 2013 at 07:19 PM

What are you planning on doing with the game objects? If you just want to be able to access them you really don't need the itemsListArray at all. You can just reference the itemsSlots game objects directly.

The answer to your question is that you will have 1, 2, 3 in your itemsListArray. This seems kind of odd that you would need an array full of integers. Those integers don't really help you get to the game objects.

Maybe if you explain what you are trying to do we can help you better.

Comment
Add comment · Show 3 · 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 TrewSx · Apr 06, 2013 at 07:38 PM 0
Share

i want to create a Loot window which will generate those 3 items as a loot in a form of GUI buttons and if clicked move this item into the inventory. For the loot window i have got this function

function Loot

     for(var _cnt : int = 0; _cnt < itemsListArray.Count; _cnt++) {
     
     if(GUI.Button(new Rect(5 + (buttonWidth * _cnt), 5, buttonWidth, buttonHeight),itemIcon)) {
 
        print("clicked on item " + itemsListArray[_cnt]);
        player = GameObject.FindWithTag ("Player");
        var items : SomeScript = player.GetComponent(SomeScript);
 
        items.AddItem(itemsListArray[_cnt]);
        itemsListArray.RemoveAt(_cnt);

I thought that i must store all prefabs in new array and then iterate through this Array to create that many buttons and move those prefabs into the inventory Do you understand what i am trying to say ?

avatar image AlucardJay · Apr 06, 2013 at 08:51 PM 0
Share

dammit, I just clicked delete ins$$anonymous$$d of edit, sorry. So, I think I said something like :

I agree with the answer, but if you want to store items in a list to add and remove inventory items from, definitely check out this link for information on using all different types of collection methods : http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

 #pragma strict
 
 // to use List
 import System.Collections.Generic;
 
 var itemsSlots : GameObject[];
 var itemsListArray : List.< GameObject >;
 
 function SomeFunction() 
 {
     itemsListArray = new List.< GameObject >();
         
     for(var i = 0; i < itemsSlots.Length; i++) 
     {
         Debug.Log( i + " for loop = " + itemsSlots[i] );
         
         // add the ITE$$anonymous$$ to the List
         itemsListArray.Add( itemsSlots[i] );
     } 
 }
avatar image TrewSx · Apr 06, 2013 at 09:34 PM 0
Share

Cool stuff i got the item in my Debug.Log so i am storing all my items now.

In another script i have a function AddItem which is adding this item from loot to inventory and it takes one parameter type of an int. I am calling this function through GetComponent and i tried to use a List as a parameter but it did not work, i changed List to a GameObject and it seems to work now.

Great help, thank you

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

12 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

Related Questions

C# SetActive GameObject Array 2 Answers

Can't add GameObjects to ArrayList 1 Answer

Assigning a GameObject to a class 1 Answer

How to create an array of Game Objects with different tags. 1 Answer

A node in a childnode? 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