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 /
avatar image
0
Question by Misthra-Games · Aug 23, 2016 at 07:40 AM · instantiatelist

How to find index of a list?

'm trying to instantiate a GameObject that is inside of a list, but in order to instantiate it i have to know it's index. How do i do this?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
     public GameObject stick;
     public GameObject stone;
 
 
     private GameObject[] itemIndex;
     private List<GameObject> inventory;
     /*
      Item 0 = stick
      Item 1 = stone
      */
 
     void Start () {
         itemIndex = new GameObject[] {stick, stone};
         inventory = new List<GameObject> {};
     }
 
     public List<GameObject> addItem (int i) {
         inventory.Add (itemIndex[i]);
         return inventory;
     }
 
     public List<GameObject> dropObject() {
     //    Instantiate ();
         inventory.RemoveAt(0);
         return inventory;
     }
 
     void Update () {
 
     }
 }
 
Comment
Add comment · Show 2
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 ashleyjlive · Aug 25, 2016 at 10:56 AM 0
Share

Have you tried inventory.IndexOf()?

avatar image Grendl · Aug 25, 2016 at 01:35 PM 0
Share

Can you clarify what exactly you want to accomplish? It seems to me you're over-complicating things.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mohammad-alavi-74 · Aug 25, 2016 at 01:34 PM

you would have to use a Predicate <GameObject> to find your desired GameObject so try this:

 public List<GameObject> dropObject(GameObject gameObjectYouWantToDrop) {
            //if you want to find the gameObject itself, just replace FindIndex with Find
              Vector3 myLocation = new Vector3 (x,y,z);
              int indexOfYourGameObject = inventory.FindIndex(x => x.Equals(gameObjectYouWantToDrop));
              Instantiate (inventory[indexOfYourGameObject] , myLocation);
              inventory.RemoveAt(indexOfYourGameObject);
              return inventory;
          }









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 mohammad-alavi-74 · Aug 25, 2016 at 02:20 PM 0
Share

however in this case you also could use inventory.IndexOf(gameObjectYouWantToDrop) which seems an easier solution as @ashleyjlive commented

avatar image JonasGamer075 · Jun 29, 2021 at 02:42 AM 0
Share

Thank you!

avatar image
1

Answer by Ali-hatem · Aug 25, 2016 at 12:12 PM

 int stickIndex,stoneIndex;
 void Update () {
      stickIndex = inventory.FindIndex(d => d == stick.gameObject);
      stoneIndex = inventory.FindIndex(d => d == stone.gameObject);
  }

or you can put the code any where if not necessary in Update.

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

Answer by roman_sedition · Jun 29, 2021 at 05:11 AM

Might be beyond what you are trying to achieve, but situation arises that you are accruing multiple of the same kind of object, so you might eventually want to consolidate your inventory, e.g you are carrying 45 stones, and 50 sticks, you want to drop that item, it doesn't really matter where in the inventory it is located, you just want to remove a specified item from it.

Consider using a Dictionary

     Dictionary<GameObject, List<GameObject>> inventory = new Dictionary<GameObject, List<GameObject>>();
 
     public void AddItem(GameObject item)
     {
         if (!inventory.ContainsKey(item))
         {
             inventory.Add(item, new List<GameObject>(){item});
         }
         else
         {
 
             inventory[item].Add(item);
         }
     }
 
     public void DropObject(GameObject item)
     {
         if (inventory.ContainsKey(item))
         {
             Instantiate(item);
             inventory[item].RemoveAt(0);
             if (inventory[item].Count == 0)
             {
                 inventory.Remove(item);
             }
         }
     }

So if the item you are picking up doesn't exist already in the dictionary, add it to the dictionary, if the gameobject key already exists in the dictionary it gets added to gameobject list for that key.

When you remove it, if it doesn't exist in the dictionary, it won't do anything. If it does it will subtract it from the gameobject list associated to that key associated to that gameobject, if the list becomes empty when you remove it, it the key gets removed from the dictionary.

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 roman_sedition · Jun 29, 2021 at 05:27 AM 0
Share

omg can't believe I necro'd this

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

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

A node in a childnode? 1 Answer

Destroying a GameObject w/ children isn't properly destroying everything? 1 Answer

Loop through a GameObject list based on the size of the list 1 Answer

Fill list with gameobject? 0 Answers

Instantiate to temp var or directly adding to list? 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