- Home /
Toggle between scripts with a script?
Hello there. My camera for a third person shooter has two scripts: when you are aiming and when you are not. My question is, is it possible to make a script that toggles between these two scripts with a press of a button? I use c# as scripting language. Thanks in advance.
Answer by weitay · Dec 18, 2014 at 07:39 AM
use the GetComponent() function, it can look for scripts as well.
So for example
public TestScript scriptReference;
scriptReference = GetComponent<TestScript>();
http://docs.unity3d.com/ScriptReference/Component.GetComponent.html
Thanks, but how do I enable/disable the script? There is no ScriptReference.enable in c# as far as I know.
Try :
scriptReference.enabled = true;
scriptReference.enabled = false;
Answer by Graham-Dunnett · Dec 17, 2014 at 10:11 PM
Yes, it is possible.
To elaborate slightly on Grahams answer... You need to find both of the scripts in question in your Toggle Script(Find the objects they belong to and the get the script component for each one).
Now in your Toggle script, when the keypress event happens, check if one script is enabled and if so disable that script and enable the other. Reverse this if not...
But how do I find the script component? Best way to do this would likely be to assign the script to a variable, but what is the argument to assign it? Thanks in advance.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Adding a listener to Toggle.onValueChanged via Script 3 Answers
[C#]Toggle Run on/off 2 Answers
No Glitch Please ... 1 Answer