- Home /
enable/disable script from another script c#?
much say it's easy, but I do not understand achievement. unity will not let me compile and gives me an error .. this is the script I want to do. function it has is that the press of a button, x script is activated and another off
this is script :
using UnityEngine;
using System.Collections;
public class nitro : MonoBehaviour {
public Component N20;
public Component Setup;
void Start(){
N20 = GetComponent<N20>();
Setup = GetComponent<Setup>();
}
void Update(){
if(Input.GetButtonDown("nitro")){
N20.enable = true;
Setup.enable = false;
}
else
{
if(Input.GetButtonUp("nitro")){
N20.enable = false;
Setup.enable = true;
}
}
}
}
^^ thanks :)
i have a doubt... if we disable the script, will all the methods used in it be disabled? Like all the coroutines too?
Answer by Rahazan · Oct 04, 2013 at 02:25 AM
Instead of
public Component N20;
public Component Setup;
You could give these fields the type they are (which is probably what you wanted in the first place).
Here's how:
public N20 n20;
public Setup setup;
N20 is the class name (type), n20 is the name we are giving this variable.
And then you would use
//.enabled instead of .enable
n20.enabled = true;
to enable the script.
Note 1: You will also have to change your Start function (as the names of the fields have changed (N20 is now n20)).
Note2: By convention class names start with a capital letter, for better readability consider changing "public class nitro" into "public class Nitro".
THAN$$anonymous$$S! was so simple, now understanding the method, thank you very much!
Answer by aldonaletto · Oct 04, 2013 at 02:42 AM
You must declare a script as MonoBehaviour (base class) or as its actual type, which's just the script's file name without any extension - and never give to a variable a name equal to some existing type (this may cause incredible confusions!). Furthermore, the property that enables a script is enabled, not enable.
Supposing that the two scripts are called N20.cs and Setup.cs, for instance, you should write something like this:
using UnityEngine;
using System.Collections;
public class nitro : MonoBehaviour {
public N20 scriptN20;
public Setup scriptSetup;
void Start(){
scriptN20 = GetComponent<N20>();
scriptSetup = GetComponent<Setup>();
}
void Update(){
if(Input.GetButtonDown("nitro")){
scriptN20.enabled = true;
scriptSetup.enabled = false;
}
else
if(Input.GetButtonUp("nitro")){
scriptN20.enabled = false;
scriptSetup.enabled = true;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
hide object start script 4 Answers
How to Stop running after a few Seconds ? 1 Answer
How can i create anouther jump script? 0 Answers
i need some code suggestions 1 Answer