• 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 XBigGuyX · Jun 28 at 01:09 PM · gameobjectvrunity 2dlistscriptableobject

VR: Put item in hand from List

Hey guys, I hope you are doing well.

So I am creating a VR with XR game in which I have a list of items as Scriptable Objects. Now I want to put one item in my hand when I press a button but I run into problems because my function is not doing the right thing. This is how it looks like:

 public void PutItemInHand()
 {
     for (int i = 0; i < inventoryItems.Count; i++)
     {
         if (inventoryItems[i].name == "Sword")
         {
             Instantiate(inventoryItems[i], handSpawn.position, handSpawn.rotation);
         }
         else if (inventoryItems[i].name == "Gun")
         {
             Instantiate(inventoryItems[i], handSpawn.position, handSpawn.rotation);
         }
     }
 }

Also it says: "Cannot instantiate a ScriptableObject with a position and rotation".

Do you guys have an idea? I would be grateful for any help. Kind regards

Comment
Add comment · Show 5
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 tormentoarmagedoom · Jun 28 at 01:38 PM 0
Share

WE need more info..

how is inventoryItems[] defined, and how you assigng the prefabs?

"run into problems because my function is not doing the right thing"... tell the results, dont just say do not work...

I dont get the rpopouse of this code... Itds a for sentence, for a list, and it instantiate different things in the same place in each iteration....i dont get it. If want to put one item in your hand, why do you use a for ??

avatar image tormentoarmagedoom tormentoarmagedoom · Jun 28 at 01:39 PM 0
Share

Ah, and that you are doing it for VR, is 100% irrelevant... Did you look on google "Cannot instantiate a ScriptableObject with a position and rotation" or "How to instantiate from scriptable object"?

avatar image XBigGuyX tormentoarmagedoom · Jun 28 at 02:04 PM 0
Share

Thanks for the replay!

Oh right, inventoryItems looks like this: public List inventoryItems = new List(); And I don't have any prefabs in that case lol :D

The problem of the function is that both items are in the list and my loop would go into both if-cases and I don't know how to do it properly. In the beginning I have no item in my hand and by pressing a button the UI of the inventory pops up and by clicking on the button of the item I want to have it in my hand. Is it really irrelevant? It needs to "replace" my hand and rotate the way my hand rotates.

Also, I need to put it back into the inventory and in another case replace it with the other item. I don't know how to do it to be honest.

In the "Item" class I have the values of my ScriptableObjects. I already looked it up and people say that ScriptableObjects can't be instantiated basically and that I need to use normal GameObjects but is that the solution?

Do these information help?

Sorry that my question wasn't formulated detailed enough :D

avatar image wideeyenow_unity XBigGuyX · Jun 28 at 02:21 PM 0
Share

As far as pulling them in and out of inventory? look up a tutorial on Object Pooling, that way the objects always exist, but only render or function when needed. Constantly deleting items and re-instantiating them is a severe waste on performance and garbage collection.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wideeyenow_unity · Jun 28 at 01:54 PM

I've only begun to play around with ScriptableObjects, but as far as I can tell they're basically a parent class. So hopefully you've set your objects to be children, and aren't using only the parent class. But one error I can see already, is you haven't defined a parent. So you'd make the object, but it'd stay where ever it spawned. You might wanna declare the hand the parent(if it's separate object?) or declare the object so you can reference it later:

 public GameObject rightHandObj;
 
 
 void SpawnInRightHand()
 {
     rightHandObj = Instantiate(List[i], handPos, handRot, parentObject);
     // or no parent if coding it's values
 }
 
 void HoldRightHand() 
 {
     rightHandObj.SetPositionAndRotation(handPos, handRot);
 }

^or something like that

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 XBigGuyX · Jun 28 at 02:55 PM 0
Share

Thanks for the answer!

What if I replace the ScriptableObjects with a prefab or normal GameObject? Would that work?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Game Object Not Working Properly 0 Answers

Can't add GameObjects to ArrayList 1 Answer

instantiate ScriptableObject, add instance to a List 1 Answer

Keep adding targets to a list 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