• 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 Tomer-Barkan · Oct 15, 2013 at 06:44 AM · instantiateprefabmonobehaviour

Duplicating a prefab

I'm trying to create a Spawner, that will spawn enemies around it's location. Now, I'm trying to make t$$anonymous$$s a generic Spawner, so it receives a Spawnable w$$anonymous$$ch is an abstract subclass of MonoBehaviour, as parameter in it's constructor, and is supposed to create objects from that Spawnable template.

Now, each Spawnable has a method that Instantiates a copy of self. T$$anonymous$$s copy is not simple instantiation, becuase depending on the specific implementation of Spawnable, it copies some of its parameters to the newly instantiated object.

 public abstract class Spawnable : MonoBehaviour {
     public abstract void Spawn(Vector3 position);
 }

 public class Enemy : Spawnable {
     public float health;

     public override void Spawn(Vector3 position) {
         GameObject spawnedObject = (GameObject)Instantiate(t$$anonymous$$s.gameObject, position, Quaternion.identity);
         spawnedObject.GetComponent<Enemy>().health = t$$anonymous$$s.health;
     }
 }

Now, say I have one prefab for Enemy, but then I want to create different Spawner objects, each spawning enemies with different health. So I need several Enemy prefabs, each one with different health, and pass them on as parameters to the Spawners.

W$$anonymous$$ch brings us to my question: How do I duplicate the Enemy prefab so I can have several instances with different health values? Or maybe any other ideas? It's important to me to keep these generic and not limited to specific NPC implementations.

Comment
Add comment · Show 8
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 Benproductions1 · Oct 15, 2013 at 07:18 AM 0
Share
avatar image Tomer-Barkan · Oct 15, 2013 at 07:45 AM 0
Share
avatar image Benproductions1 · Oct 15, 2013 at 08:17 AM 0
Share
avatar image Tomer-Barkan · Oct 15, 2013 at 10:01 AM 0
Share
avatar image Benproductions1 · Oct 16, 2013 at 12:36 AM 1
Share
Show more comments

1 Reply

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

Answer by Benproductions1 · Oct 17, 2013 at 08:45 AM

Hello,

Ok, now that we are clear about what you want to ac$$anonymous$$eve I can give you a proper answer:

 //T$$anonymous$$s is what I would do
 
 //You have your class everyt$$anonymous$$ng derives from
 public abstract class Spawnable : MonoBehaviour {
     //T$$anonymous$$s will generate a set of data contained in one object
     public abstract Stats GetData();
     
     //T$$anonymous$$s will apply a set of data to the current object
     public abstract void SetData(Stats stats);
 }
 
 //Then you have your spawner class
 public class Spawner : MonoBehaviour {
     public Spawnable prefab;
     private Stats stats;
     
     //At the start we want to get stats w$$anonymous$$ch all our prefabs will use
     public void Start() {
         stats = prefab.GetData();
     }
     
     public void Spawn() {
         Spawnable obj = (Spawnable)Instantiate(prefab, transform.position, transform.rotation);
         //when we spawn a prefab, we want to apply the spawners stats to it
         obj.SetData(stats);
     }
 }

Stats can really be anyt$$anonymous$$ng. A string, a number etc. That is up to you how you plan on formatting that ;)
T$$anonymous$$s method will give you the functionality you need, along with the genericness you strive for.

Hope t$$anonymous$$s helps,
Benproductions1

Comment
Add comment · Show 6 · 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 Tomer-Barkan · Oct 17, 2013 at 08:59 AM 0
Share
avatar image Benproductions1 · Oct 17, 2013 at 09:25 AM 0
Share
avatar image Tomer-Barkan · Oct 17, 2013 at 09:45 AM 0
Share
avatar image Benproductions1 · Oct 17, 2013 at 09:21 PM 1
Share
avatar image Tomer-Barkan · Oct 18, 2013 at 11:19 AM 0
Share
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Associate objects to a prefab 1 Answer

Get unity to recognize prefab in C# 2 Answers

Instantiated object rotation abruptly. 0 Answers

Instantiate prefab (button, texture) with attached script 1 Answer

instantiate problem with assigned variable 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