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 topkekker · Oct 22, 2014 at 11:15 PM · c#listinventory

Unity add a item to a list?

Hey guys. So im having some trouble.

What im trying to do is add a item called testCanteen from my assets into a list called inventory.

list:

 List<GameObject> inventory = new List<GameObject>();

adding item:

 inventory.Add(new GameObject("canteenTest"));

Now im pretty sure with the quotes it wont create the object i need as it says it doesnt exist.

So what im wandering is how do i add a item from the assets bar into a list so i can then instantiate it and parent it to the players camera?

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

2 Replies

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

Answer by MarksTeq · Oct 22, 2014 at 11:44 PM

  1. Create a canteen prefab.

  2. Code something like:

    using UnityEngine; using System.Collections; using System.Collections.Generic;

    public class Canteen : MonoBehaviour { List inventory = new List(); } Drag your canteen prefab into an object slot:

alt text

This should allow you to instantiate any prefab in the list.

Then instantiating and parenting to camera can look something like:

     void MakeCanteen()
     {
         Instantiate(inventory[0]);
         inventory[0].GetComponent<Transform>().parent = Camera.main.transform;
     }



unity_answer_inventory.png (5.8 kB)
unity_answer_inventory_2.png (9.6 kB)
Comment
Add comment · Show 4 · 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 topkekker · Oct 22, 2014 at 11:55 PM 0
Share

Ignore my previous comment, I didn't use my eyes correctly, This was the perfect solution. Thank you VERY much!

avatar image Kiwasi · Oct 23, 2014 at 12:16 AM 2
Share

You can do this faster by just adding the prefabs to a public list directly via the inspector.

avatar image MarksTeq · Oct 23, 2014 at 12:33 AM 0
Share

@Bored$$anonymous$$ormon You sir are a genius and a scholar! Edited :)

avatar image Habitablaba · Oct 23, 2014 at 12:40 AM 0
Share

TIL: unity answers will sometimes refresh the comments on an answer, but not the answer itself.

avatar image
0

Answer by Habitablaba · Oct 22, 2014 at 11:36 PM

Drag the object you want into the edit area and tweak it to be like you want it.
Then, drag it back to the project pane in order to turn it into a prefab.
Now remove the object you put into the scene, as you don't need it any more.
Next, in the script, add a member variable of type GameObject, and drag the prefab over to it.
Finally, call Instantiate() to create a new version of the prefab, twiddle the bits to get it how you want it for this instance, and shove that into your list.

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 topkekker · Oct 22, 2014 at 11:46 PM 0
Share

What do you mean by a member variable?

avatar image Habitablaba · Oct 22, 2014 at 11:54 PM 0
Share

I mean a variable at class scope. If this is something foreign to you, I strongly suggest you spend some time watching tutorials and reading up on the basics of program$$anonymous$$g in modern languages like C#

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to ignore base class? 1 Answer

Lists as Parameters? 1 Answer

check if list contains item with matching string property 2 Answers

Method is called, but GUI doesn't show up 1 Answer

Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED] 2 Answers


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