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 greens356 · Jun 13, 2020 at 11:02 PM · scripting problemscriptableobjectinventoryinventory systemscriptable object

How to Instantiate objects that are Scriptable Objects

Hi I am trying to follow this tutorial (http://toqoz.fyi/unity-painless-inventory.html) as a reference for my inventory system. I am not implementing the exact same way due to some design considerations so am trying to adapt it to my situation.

My previous issue was that game objects were not persisting between scenes. the game objects that I wanted to persist, I added a DontDestroyOnLoad() into their scripts awake() methods and then that allowed me to then have the items I wanted in the next scene, but then I had to write code to manually delete the game objects from the 2nd scene that I did not want to be there as they ALL came over and it doesn't make sense for context of game. This is extra work and I see a lot of talk about Scriptable objects that as I understand should allow me to have a "static" inventory and then independent of which scene I am in, I can just instantiate items from the inventory in the scene I am in as needed. Also I want to be able to save player game data in-between play sessions and I hear that the Scriptable Objects will allow me to do this easier so anticipating this work I am trying to set myself up for success later there.

So to avoid having to manage the clean up effort of deleting game objects that do not belong between scenes I tried to implement Scriptable Objects with a Item.cs : ScriptableObject (similar to tutorial above) for all items that essentially then are purchased from a vendor in one scene, are then added to a ScriptableObject Inventory system and then I wanted to then instantiate an object for every item in the inventory in the next scene. The problem is I want to now instantiate these items but I cannot put a position or rotation on a scriptable object, so I have been trying to in code, create an instance of an Item.cs class and then use its associated GameObject field to show its physical representation. The problem then is I have to set each of its components (Box Collider, OVRGrabbable, etc..) and all the values I want them to have so I tried using a method I found on here for deep copy of component values (https://answers.unity.com/questions/530178/how-to-get-a-component-from-an-object-and-add-it-t.html?_ga=2.9981105.527810571.1592029078-1716816744.1582330304) and it is complaining about:

"Can't add component 'Component' to NewGameObject because such a component is already added to the game object!" and also
NullReferenceException UnityEngine.Component.get_tag () (at :0) System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at :0) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke ...

So I don't know that I am going the best away about this but I don't know any better at the moment so I understand its a lot to digest and this points at bigger architectural/design questions in the game so I would love help with talking through these considerations and changing to what is better implementation but if not, then making it work and maybe I learn more later and refactor again..


SpawnSaleItems.cs

...

   void Start()
 {

     GameObject go = new GameObject("NewGameObject");
     go.transform.position = new Vector3(1, 1, 1);

 //gameTemplate is a prefab GameObject that has all the components and values I want to copy over for each instance of an Item
     foreach (Component comp in gameTemplate.GetComponents(typeof(Component)))
     {
         Component c = new Component();
         c.GetCopyOfComponent(comp);
         go.AddComponent(c);
     }

...

     commonGames = new List<Item>();
     commonGames.Add(new Item("Super Mario Bros", References.Console.NES, Resources.Load<Sprite>("NES/SuperMarioBros"), 8, References.ItemCondition.Great, References.ItemState.Functional, go));

....



Item.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class Item : ScriptableObject { int autoIncrementIdCounter = 0;

 public int rowItemId;
 public Sprite itemImage;
 public string itemName;
 public int itemPrice;
 public References.Console itemConsole;
 public References.ItemCondition itemCondition;
 public References.ItemState itemState;
 public GameObject physicalObject;

 public Item(string name, References.Console console, Sprite img, int price, References.ItemCondition cond, References.ItemState state, GameObject phys)
 {
     rowItemId = autoIncrementIdCounter++;
     itemImage = img;
     itemName = name;
     itemPrice = price;
     itemConsole = console;
     itemCondition = cond;
     itemState = state;
     physicalObject = phys;
 }

}

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

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

234 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

Related Questions

Scriptableobject List and Instantiating objects from it 3 Answers

Proper item usage design 0 Answers

Problems calling functions from Scriptable Objects 0 Answers

Referencing a class defined within a ScriptableObject 0 Answers

Need some advice for my inventory system 0 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