- Home /
Can I add a script into another one to manipulate it's variables in the inspector?
Hello everyone.
I'm quite new with the usage of Unity and i got a slight doubt.
I'm trying to make a game in which a player can step on a mine and get an effect from it, each one with different variables (one effect inflicts damage, another snares..).
So far i had a GameObject with the 'Mine' script on it, and a variable for the effect:
public class Mine extends MonoBehaviour {
public var healthTotal : int;
public var type : ElementEnums.Type;
public var damage: int;
public var effect : Effect;
public function Awake() {
super.Awake();
}
/*
code
*/
}
And the effects extending a common class:
public class Effect {
public var duration : int;
public var texture: CustomTexture;
public function Effect() {
/*
code
*/
}
/*
code
*/
}
public class Burn extends Effect{
public var damage: int;
public function Burn() {
super();
/*
code
*/
}
/*
code
*/
}
With this, in the inspector i can only see the common effect variables
I wanted to make it possible to exchange the effects quickly (dropping in the script same way the a GameObject can be dropped and changing the variables of that script. Kinda like the drop menu of the type).
I've tried it the Effect script extend MonoBehaviour and applying it to a GameObject:
public class Effect extends MonoBehaviour {
public var duration : int;
public var texture: CustomTexture;
public function Effect() {
/*
code
*/
}
/*
code
*/
}
This way i can drop each effect in a different object and then drop the object in the mine, but it creates a big ammount of objects in the hierarchy each time that it's triggered the OnEnter event, what i presume that would be bad for the performance the more that it's played :/
I also though of creating different mines, one per effect:
public class BurnMine extends Mine {
public var effect : BurnEffect;
/*
code
*/
}
public class SnareMine extends Mine {
public var effect : SnareEffect;
/*
code
*/
}
But would be a way to do it keeping the generic idea of "create a Mine, drop in the effect wanted" and without relying on GameObjects for the effects?
Answer by sysameca · Nov 02, 2014 at 07:34 PM
If i was coding this i would prefer exactly the second thing you thought - the Mine class to be the base class of all mines and inherit from MonoBehavior - (Mine : MonoBehavior). From there extend it to SnareMine : Mine, FlamingMine : Mine and etc. Then when creating the new mine game object, just assign the type of mine i want it to be. It's very simple and simplicity is your friend. From there all the extended class variables will be shown in the inspector.