- Home /
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! :)
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.
@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?
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.
@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
Follow this Question
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