I have an update function that will check if something is equal to true and if it is equal to true it will run some code but then I want the whole script to be disabled. Please help, thanks!
Asked
Active
Viewed 4.1k times
4
-
You can try `enabled = false;` – Paweł Marecki Feb 29 '16 at 13:40
-
Nope, doesn't work. Or could you please explain it a bit more? – alexo1001 Feb 29 '16 at 13:43
-
Adda public bool called enabled, when you want to disable it set it to true, and in the main code execution check if it's true, if that's the case exit doing nothing. – Gusman Feb 29 '16 at 13:45
-
Ok so can you show us code? – Paweł Marecki Feb 29 '16 at 13:45
-
Yes, this is the code. https://github.com/AreoBlazeStudios/MINI-TANKS/blob/master/EmailSender.cs – alexo1001 Feb 29 '16 at 13:48
-
1Possible duplicate of [How do I enable Parts/Components in Unity C# with only a game object in the script](http://stackoverflow.com/questions/31993266/how-do-i-enable-parts-components-in-unity-c-sharp-with-only-a-game-object-in-the) – Fattie Feb 29 '16 at 13:54
-
Answerers and Commentors, you can most help and get the most points by just clicking "Close as a duplicate" and finding a duplicate – Fattie Feb 29 '16 at 13:55
3 Answers
10
You should use an enabled field. Try following
gameObject.GetComponent<EmailSender >().enabled = false;

Valentin
- 5,380
- 2
- 24
- 38
-
What do you mean, it doesn't work? What do you expect it to do and what does it do? – HoloLady Feb 29 '16 at 14:00
-
-
0
This will work:
public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
}

Eric Aya
- 69,473
- 35
- 181
- 253

Kamran Mohazzab
- 11
- 1
0
You can disable script component by using the following syntax,
GameObject.Find("Cube").GetComponent<MoveObject>().enabled = false;
Here I disable the MoveObject script from the object named Cube.

Codemaker2015
- 12,190
- 6
- 97
- 81