Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PseudoDev · Feb 06, 2016 at 03:42 PM · parametersconstructors

Instantiate scriptable objects with optional amount of specified parameters

I have a fruit object:

 public class Fruit {
     public string fruitName;
     public string family;
     public string species;
     public List<string> tastes = new List<string>();
     public List<string> behaviour = new List<string>();
     
     public Fruit (string newName, string newFamily, string newSpecies) {
         fruitName = newName;
         family = newFamily;
         species = newSpecies;
     }
 }

I have a fruit controller to instantiate fruits:

 public class FruitController : MonoBehaviour {
     public List<Fruit> fruits = new List<Fruits>();
     
     void Awake (){
         fruits.Add (new Fruit ("Apple", "Rosaceae", "Malus Domestica"));
         fruits[0].tastes.Add ("Sweet", "Sour", "Bitter", "Earthy");
         fruits[0].behaviour.Add ("Happy", "Passive-Aggressive", "Cromulence");
     }
 }

Is there any way of instantiating fruit within a single line and not having to continually add to the 'tastes' and 'behaviours' lists of each fruit following instantiation? Is there a better way than using lists entirely? Ideally, I would want to be able to write as little or as many of these attributes when instantiating fruit, but only in a single line. For example:

 //Parameters: fruitName, family, species, taste1, taste2, taste3, taste4, behaviour1, behaviour2, behaviour3
 fruits.Add (new Fruit ("Apple", "Rosaceae", "Malus Domestica", "Sweet", "Sour", "Bitter", "Earthy", "Happy", "Passive-Aggressive", "Cromulence"));
 
 //Parameters: fruitName, family, species, taste1, behaviour1, behaviour2
 fruits.Add (new Fruit ("Pomegranate", "Lythraceae", "Punica Granatum", "Bloody", "Flatulent", "Destructive"));

As you can see, the amount of entries differ between the two fruits in the two parameters of tastes and behaviours. Obviously, this doesn't work as is, since there's no differentiation between tastes and behaviours in the original Fruit class -unless I specifically overload the constructor, but this would again limit how many of each taste or behaviour I can add via instantiation- and the compiler has no idea what I'm doing. I'm just wondering if there's some implementation that allows for this sort of thing. I'd also be open to suggestions for not using Lists for these parameters, but they seem to work as is, albeit with the annoyance and potentially gargantuan task of having to write thousands of lines of code for insertion of said unknown amount of attributes, and is the only thing I can really think of. I would like many fruits by the end of it.

Thanks.

Comment
Add comment · Show 2
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 meat5000 ♦ · Feb 06, 2016 at 03:41 PM 0
Share

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/polymorphism?playlist=17117

avatar image Teravisor · Feb 06, 2016 at 10:18 PM 1
Share

You can have Lists(or better HashSets for performance of Contains) of all possible variants and then check each string received from constructor public Fruit(string newName, string newFamily, string newSpecies, params string[] values) if they are tastes or behaviours or anything else using something like allTastes.Contains(values[a]). params keyword allows any amount of parameters to be received as an array ins$$anonymous$$d.

1 Reply

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

Answer by PseudoDev · Mar 08, 2016 at 06:47 PM

Thanks for the answer and apologies for the lateness of my reply.

I found these topics with similar functionality:

optional parameter string , Adding Multiple Elements to a List on One Line

It looks like params is what I need.

Edit: I have no idea as to why I can't pick Teravisor's reply as the answer. I can 'convert' my reply to his as the answer. Very strange.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Placement Question. 1 Answer

accessing custom url parameters on android 1 Answer

Unityscript how to change parameters value 1 Answer

Reflection - passing parameters to an invoked function 2 Answers

space wrap 3D 1 Answer


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