Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Inventory/Item issues. Need solution to items without gameObjects.

Discussion in 'Scripting' started by Pysassin, Apr 29, 2014.

  1. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Hey all sorry I suck with titles. What I have is a FPS/RPG project going on. Currently working on weapon customization but the issue will soon arise with many other aspects on my project atm. I have a class system I use to store information on items in your inventory. because they are in your inventory (script based as well) and not really needed in scene I don't have those classes attached to an actual gameObject. Thing I need my script to do is be able to check if it is on a gameObject at all and if it isn't instantiate a clone of its prefab (a variable set in the class script) and then transfer all the class information from the script running to the script on the prefab. This would also apply to if I had to drop an item from my inventory as well. I'd need to make an object since previously it wasn't attached to one. If this is confusing I apologize but I'm stuck and would appreciate any ideas/suggestions.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1. if(gameObject.GetComponent<MyScript>() == null)gameObject.AddComponent<MyScript>();
    2. MyScript myScript = gameObject.GetComponent<MyScript>();
    Should do the trick.
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1.  
    2. public class ItemDefinition : ScriptableObject
    3. {
    4.     public ItemStat stat;
    5.     public GameObject prefab;
    6. }
    7.  
    8. [Serializable]
    9. public class ItemStat
    10. {
    11.     // Some stats
    12. }
    13.  
    When you create your visual, you pass ItemStat from the ScriptableObject to the MonoBehaviour.
     
  4. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Sorry I'm rather new, what does Serializable do? Also does the visual GameObject prefab need to have that component on it already? Also not sure how to get ItemClass component on Item A equal to ItemClass component NOT on ANY gameObject but in the inventory's list.

    Edit: I read up a bit and THINK I know what you are saying. Instead of applying the ItemClass component to the mono, use a seperate container component that has a ItemClass variable in it. Then just make the variable equal to the inventory item?
     
    Last edited: Apr 30, 2014
  5. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Don't think this would work because neither object has the itemclass on it as a variable. The prefab mono has the itemClass on it (extends mono). and the inventory has an itemclass variable (not the component). If I'm understanding yours correctly your showing the opposite of what I would need.
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Pretty much make your item stat independent from your prefabs.
     
  7. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    For some reason, When I first started, serialization worked, and displayed as expected. When I extend the base class though it looses the effect. Now nothing is showing up correctly in the inspector.... What is the proper syntax in UnityScript? I've been using @SerializeField . Is that correct?
     
    Last edited: May 2, 2014
  8. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Well I found that it stopped working because I extended monobehaviour which I need the extended classes to have access to since I need to reference a transform in them... There a way to still see the stats(serialize the whole class) while still extending mono? (for some reason extending System.Object does not allow me access to transform...)

    EDIT: I'm going to log for the night as I am making STUPID mistakes and asking even worse questions. I just made a variable that stores my transform that is needed and can pass it in through the mono's class. Thanks Striker for all your help man. when I am more awake it will be really helpful lol.
     
    Last edited: May 2, 2014