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 jamespaxi · Jul 24, 2013 at 06:10 PM · prefabassetsubclass

Prefab subtypes - Any methods considered good practice?

Hi all,

I have a basic Asset called Car, with basic Scripts and Objects attached to it.

I also have a list of modified versions based on the Car Asset. e.g. Sports Car, SUV, Family Car, Small Car, ...

I wouldn't want to create individual Prefabs, to later have to modify the Car functionality in each one. Imagine I have 15 different Car prefabs.


What would you do? Any ideas? 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
3
Best Answer

Answer by Jamora · Jul 24, 2013 at 06:42 PM

I would use the Factory design pattern here. Basically, you create a static Factory script, which you can then ask to create any type of car you have. This is especially useful if you need to instantiate lots of cars during runtime, but less so if you want to place them in your scene via the scene view, you'll need the prefabs for that.

 using UnityEngine;
 using System.Collections;
 
 public static class CarFactory {
 
     public static GameObject CreateSportsCar(){
         GameObject result = new GameObject("Sportscar");
         //add whatever meshes or other components here
         return result;
             //Or if you do create prefabs, just instantiate them here instead
     }
     
     public static GameObject CreateSUV(){
         GameObject result = new GameObject("SUV");
         //add whatever meshes or other components here
         return result;
     }
 }

As for the functionality for your cars, I imagine you mean code. I would create an abstract class Car, which is then inherited to all other types. This way, you don't have to go through all scripts if you just want to change a default value in all cars.

 using UnityEngine;
 using System.Collections;
 
 public abstract class Car : MonoBehaviour {
 
     protected float speed = 5f;
     protected int numberOfTires = 4;
     
     public abstract float GetWeight();
     
     public int GetNumberOfTires(){ return numberOfTires; }
 }
 
 public class SportsCar : Car{
     
     float weight = 200.5f;
     
     public override float GetWeight(){ return weight; }
 }

Now, if you want all your cars to have only 3 tires, instead of changing it in 15 scripts, you change it in Car. So you could create the prefabs too.

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 jamespaxi · Jul 24, 2013 at 06:52 PM 0
Share

@Jamora thanks again for the top quality answer. I'm already using inheritance within the main Script component. What kind of annoys me is the ParticleEffect and other child objects, where some sort of inheritance would be useful. Sounds like I should probably build separate prefabs for each child object and have them referenced by the Factory Script or the Car subclass Script. What do you think?

avatar image Jamora · Jul 24, 2013 at 07:26 PM 1
Share

You're not giving me a lot to work with, but I think you should make prefabs. Have them referenced from the factory so no other script knows( rather, is allowed) how to make them... helps in debugging when only one script can do something.

You don't need to create prefabs of your child objects, only the parent GameObject. When you create a prefab, the children are included in it.

Also have a read of http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/ 16-20 are about prefabs.

avatar image jamespaxi · Jul 24, 2013 at 08:21 PM 0
Share

@Jamora Thanks for the link. It covers pretty much a lot of questions I've had going on.

Things are really clear now, appreciate that a lot.

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

16 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

Related Questions

Any way to 'GetComponent' as a base class? 2 Answers

Dressing Room 0 Answers

Adding audio to asset bugs out the movement 1 Answer

Why do my prefabs not have an Asset Preview? 1 Answer

Saving usermade content 2 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