Hey guys how’s everyone doing, I’m here today looking for a little help. Okay, here’s what I’m looking to do. I have successfully scripted working elevators, lights and light switch’s they all work great. Now I would like to make them only work when a Power Supply is enabled. I’m a little stuck here because I don’t understand how to make one trigger activate another script or make one trigger toggle the activation of another trigger, I’m not sure if I said that correctly. What I mean is I would like to Activate a Power Supply switch allowing the the Light Switch to power the light. If you press the Light Switch without the Power Supply on nothing would happen other than a message telling you Power is required to turn on the light or to use elevator. Could I do all this in one script or would I possibly need two? Any help is very much appreciated.
Maybe start something like this.
public bool onTriggerPowerSupply;
public bool onTriggerLightSwitch; “If it’s even possible to have two triggers in the same script”
public bool powerSupply;
public bool lightSwitch;
public GameObject theLight;
If player tries to activate some gadget/machine which requires power, call its Activate.
Activate could then call IsPowered() to check if it has a power supply component assigned into its field and if power supply (optionally) has enough power to run the machine. If both are true, you set your machine to running state. If not, you let player know.
Your machines could also do IsPowered check periodically or every frame, so if someone unplugs the power supply, or power level falls too low, gadgets/machines get disabled automatically.
Thanks for your quick reply. I’m now looking up everything I can about interfaces. I’ve found I few books to reference but is kind of confusing. None of the examples are actually for anything like I’m trying to do. Are there any examples you know of I could check out. Thanks for your help man.
I’m going to check out the unity class on scripting interfaces in the unity learn section ASAP.
The interface method @eses suggests above is great because it is open-ended and flexible.
But you can initially accomplish the same thing just with a “PowerSupply” Monobehavior, and then dragging a reference of it into whatever needs to be powered. That would enable the elevator to reach over to that power supply and ask if it is on or not.
I was gonna suggest basically what @eses said but I was thinking of it in this way:
public class PowerSupply {
public bool isOn;
}
have a base class or interface
public class PowerConsumer {
public PowerSupply powerSupply;
public bool isOn;
public bool state { get { return isOn && powerSupply.isOn } }
}
}
and than every item can inherit or implement this
public class Toaster : PowerConsumer {
public bool hasToast;
public bool toastReady;
public float toastingTimeLeft;
public void Step(float deltaTime){ //manual update thingy
if(state == false || !hasToast || !toastReady)
return;
toastingTimeLeft -= deltaTime;
if(toastingTimeLeft < 0f){
toastReady = true;
}
}
}
I hope my code can speak for itself(if you need an explanation just ask), also if you make it this way you can easily add how much power a thing consumes to create circuit breaks when going over the supply maxPower and stuff like that.