I’m trying to put together a power up system in an efficient/modular way. I have a settings file attached to an empty game object in the scene. By plan was to define the various power ups in this settings file as follows:
#pragma strict
var powerup : Powerup;
class Powerup{
var boost : Boost;
class Boost{
var duration : float = .8;
var texture : Texture2D;
// var scriptToAttachToPlayer : (?);
}
var blast : Blast;
class Blast{
var duration : float = .8;
var texture : Texture2D;
// var scriptToAttachToPlayer : (?);
}
var hide : Hide;
class Hide{
var duration : float = 4.0;
var texture : Texture2D;
// var scriptToAttachToPlayer : (?);
}
}
Duration refers to how long the power up will remain active once triggered. Texture is a bitmap icon representation of the powerup.
What I’d like to be able to do is set things up so that I can use the inspector to drag arbitrary scripts into a scriptToAttachToPlayer variable, per powerup, and that later these scripts will be programmatically attached/removed from players when the relevant powerup is activated/deactivated. I’m not sure how to achieve this though.
Is this possible? Or should I be approaching this in a different way?
What you are looking for as a variable type is Type, but you would have to do a custom inspector to assign it, the built in one doesn’t do that. You could just have a string variable and type in the name of the script.
Also, it looks like you should be having a base class containing that duration, texture and script though - presumably other things about this would be different, but those bases would be the same.
class baseClassName
{
var duration : float;
var texture : Texture2D;
var script : String;
}
class Boost extends baseClassName
{
}
Bovine
3
For the data I have that I want to assign in this way I tend to use ScriptableObjects. This may be too heavy weight for your needs however, but it would certainly work.
There’s a tutorial here on ScriptableObjects:
http://buchhofer.com/2010/10/unity-toying-with-scriptable-objects/
I confess I have not read it, but it’s too in-depth to talk about in an answer - I throw it out there as a research topic 