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 gardian06 · Jan 05, 2013 at 09:02 PM · gameobjectinstantiateprefab

Interchangeable Objects/Prefabs

when needing things to be created into the game world I understand there are 2 options:

  1. instantiate, such as firing a gun to instantiate a bullet to destroy said bullet on impact.

  2. I know that one alternative to instantiating (as it can lead to memory fragmentation, and on mobile platforms is almost a bad joke) every time an object is to be created in the world is to find the maximum number of those things that "could" exist in your game, and then when they are needed get one that is not being used then reposition, reorient, and render it.

but at least in my mind this second approach only lends itself to be used with the same things.

for example: lets I have 3 types of bullets. the difference between the bullets will be that they each have a different child class of an abstract base class, and a different model. now all of these bullets have an equal likelihood to be created in the game, and therefore have an equal likelihood to be needed. I would like to use option2 above, but I would like to do it where I can interchange the model, and scripts. almost like interchanging the prefabs themselves.

Is this possible? How?

Comment
Add comment · Show 1
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 gardian06 · Jan 05, 2013 at 10:37 PM 0
Share

bullets were more an example of this. I was wanting to be able to do this with bullets, enemies, and so on.

essentially be able to interchange any Object of TypeX with similar. like I have all these enemies in the level ins$$anonymous$$d of destroying them ins$$anonymous$$d I just stop rendering them, and then replace them with a different type of enemy later as needed.

similar to treating the Objects/prefabs themselves as components/templates.

1 Reply

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

Answer by AndyMartin458 · Jan 05, 2013 at 09:22 PM

For the scripts specifically, by using inheritance you can maintain an array that is the maximum size which holds base class scripts. This array can hold any of the 3 child scripts as well. Perhaps this would also be the way to do what you're talking about. Create an array to hold your max amount of game objects.

From my understanding of Unity, you can't really do this with prefabs directly. An easier option (though costly in performance) would be to create the maximum amount of all 3 kinds of objects. Then no matter what, you would be able to have that many objects on the screen. However, I don't think that a game studio would follow that methodology for bullets. I think that they instantiate a bullet when it is needed and delete it when it is no longer necessary. What is the lifetime of the bullets?

Comment
Add comment · Show 3 · 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 gardian06 · Jan 05, 2013 at 10:36 PM 0
Share

see comment above.

avatar image AndyMartin458 · Jan 05, 2013 at 10:54 PM 1
Share

I mean, it sounds like an interesting idea, but I don't think that's how Unity works. It kind of violates the typical best practices.

If you look here http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html under "Replacing a character with a ragdoll or wreck", they essentially say that it's much better to delete the old one and instantiate a new prefab ins$$anonymous$$d of trying to have fancy code on the original prefab.

Say you have 3 enemy types, and you want to populate a level with 10 random enemies. Then, you create a dictionary that holds a reference to the 3 types of enemies. In Start, use the random function to instantiate an enemy and add it to an enemy array (maintain all your enemies in an array of game objects, a dictionary of the three different kinds, or however you see fit). Then you can still have a bunch of different enemies up to your maximum number (or even more), but you won't have to deal with dynamically changing your prefab.

avatar image AndyMartin458 · Feb 22, 2013 at 11:31 PM 0
Share

I'm glad this worked for your problem!

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Retrieving GameObjects from a Prefab 1 Answer

Spawning a prefab at another object's location 3 Answers

How to instantiate a prefab with a script attached? 2 Answers

I cant delete multiple instantiated prefabs 0 Answers

Object spawns itself instead of it's prefab. 3 Answers


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