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 FusionSausage · Oct 20, 2013 at 02:13 PM · instantiatelistgeneric

Get method in script with Generic list

Hi guys! Right now I'm developing a game with the ability to instantiate objects on the terrain. Now I'm closing up on 20 different buildings, which right now are all instantiated from one script with a variable for every object which is the one to instantiate, rotate and so on. Now, I would like to clean this up a bit, and my idea was to have every script and the associated building (object) in an empty GameObject, and then have a Generic list containing all scripts and then if I choose for example object 5 it should use the script object-5.cs and and then instantiate the object associated with the object 5 script. How would I do this, or is it another way?

Thanks!

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

1 Reply

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

Answer by Azrapse · Oct 20, 2013 at 02:37 PM

You can use either inheritance or delegates.

Inheritance:

Create a c# file with this contents:

 public interface IObjectSpawner
 {
     void DoWork();
 }

Then go to all your scripts that generate stuff and add that interface to their inheritance list. For example, if you have a script called TreeGenerator, edit the declaration so that it looks like:

 public class TreeGenerator: MonoBehaviour, IObjectSpawner
 {
 
     [...] // your script contents
 
     public void DoWork()
     {
         //Call here to the method in this script that does the spawning.
     }
 
 }

And finally, on the centralized place where you want to have a generic list with all the scripts that spawn objects, you can declare it like this:

 public List<IObjectSpawner> spawnerList;

And you can iterate over it and call the DoWork() method to spawn their object, for example:

 foreach(var spawner in spawnerList)
 {
     spawner.DoWork();
 }


Delegates:

Declare your list as

 public List<Action> spawnerMethods;

Add calls to the spawning methods into that list like:

 spawnerMethods.Add(myScript1Object.DoSomeWork);
 spawnerMethods.Add(myScript2Thingie.Spawn);
 spawnerMethods.Add(myScript3Stuff.MakeClone);

Where the methods DoSomeWork, Spawn and MakeClone belonging to those three scripts all have in common that are called like

 public void MethodName ()

that is, with no parameters and no return value.

Comment
Add comment · Show 10 · 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 FusionSausage · Oct 20, 2013 at 03:19 PM 0
Share

Thanks for your answer, I'm checking it out right now. Do I have to include the raycast and everything? And how do I get for example the SpawnWindmill script, if I wanted to use, let's say $$anonymous$$eycode.O? With Inheritance.

avatar image Azrapse · Oct 20, 2013 at 04:42 PM 0
Share

It depends on your particular design. I have no idea what you mean with the raycast. $$anonymous$$aybe you want to cast a ray towards the terrain, then on the point it touches the terrain you want to spawn an object depending on which key the user presses?

avatar image FusionSausage · Oct 20, 2013 at 04:53 PM 0
Share

Yes, I have a raycast method which is raycasting towards the terrain, but I have a variable called chosenObject which contains an integer, for example 5. So if I, for example, press 5 it changes the chosenObject and therefore changes the object being instantiated. So if I press 5, the list item #5 (for now) should get picked.

avatar image Azrapse · Oct 20, 2013 at 08:56 PM 0
Share

Nice. Then create each of those 10 different spawners and add them to the list. On Input, you could use a switch or something to select an index from the list. I would probably create a Dictionary where the key is a $$anonymous$$eycode value and the values are the matching index in the List.

Something like:

 protected Dictionary<$$anonymous$$eycode, int> key$$anonymous$$appings = new Dictionary<$$anonymous$$eycode, int>
 {
   {$$anonymous$$eyCode.Q, 0},
   {$$anonymous$$eyCode.W, 1},
   {$$anonymous$$eyCode.E, 2}
 [...]
 }

And then, in the Update method something like:

 foreach(var key$$anonymous$$ap in key$$anonymous$$appings)
 {
   if(Input.Get$$anonymous$$eyDown(key$$anonymous$$ap.$$anonymous$$ey)
   {
     spawnerList[key$$anonymous$$ap.Value].DoWork();
   }
 }
avatar image FusionSausage · Oct 20, 2013 at 09:00 PM 0
Share

Sounds all good to me, I'm gonna try this out tomorrow, it's kind of late over here. Thanks!

Show more comments

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

17 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

Related Questions

A node in a childnode? 1 Answer

Converting Builtin Array to Generic List 2 Answers

How to find index of a list? 3 Answers

Checking if object intersects? 1 Answer

How do we instantiate random sprites from an array... 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