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

pick from multiple classes

Discussion in 'Scripting' started by will_brett, Jan 7, 2015.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Is it possible to have multiple classes inside a script that contain a load of variables. Then in the inspector choose only one of these classes to modify variables?

    For example i create a script with two classes. Each class holds some variables. In the inspector there is a checkbox or dropdown menu so that you can choose which class of variables to modify. This hides one set and shows the other.

    The reason I want to do this or something similar is that I have a scriptable object that stores a load of values for a template shop item. (name, cost, sprite etc) however the shop items do different things depending on what the item is so some change object colours whereas others add an object to the scene. So different items require different variables.
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks Fluzing. I've had a look at that link and a few more tutorials but not really sure how to get this to work with a scriptable object for my above issue.
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    It is hard to grasp at first, but the unity tutorial explains it pretty good. Took me a few times to get the hang of it as well. Best way to learn it is just to make a new project and goof around with it.

    If you want an object to be of the same class, but be able to perform different things, then this is really the way to go.
     
  5. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    I think I get it. So the top class would have all variables that are shared then the inherited classes would have extra variables specific to that class.

    Then I would just need to modify my Scriptable object creator script to create an inherited class rather than the top class?
     
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Exactly. And it is not only variables, but also functions.
     
  7. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Code (CSharp):
    1. public class TopClass
    2. {
    3. public variable common variable
    4. }
    5.  
    6. public class Inhertited class : TopClass
    7. {
    8. public variable classSpecificVariable
    9. }
    10.  
    11. public class AnotherInhertited class : TopClass
    12. {
    13. public variable anotherclassSpecificVariable
    14. }
    15.  
    16.  
    Then say i wanted to create a scriptable object for InheritedClass
    Code (CSharp):
    1. public class NewItem  : ScriptableObject
    2. {
    3.     public List<InhertitedClass> inheritedClass;
    4. }
     
  8. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Ah ok so now I just need to figure out how to create an option in the inspector for which inherited class to choose when making a scriptable object
     
  9. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Make sure that you declare each class in its own script and your declaration is not completely correct right now.
     
  10. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    What is the reason for declaring each class in its own script?
     
  11. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Organisation/Avoiding scope errors/easier to understand code
     
  12. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks. learnt a lot today. :)
     
  13. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Oh man this works so good. Thanks again