- Home /
Disabled script still working C#
Hey there. When I click on object and object's see = true script will run IF loop. It should run just once even when Object will be clicked 10 times(I want to prevent from decreasing move points in future). So at the end of IF loop i put code that should disable all script component in that gameobject. And it works. Script is disabled but... Still woring. Still i can click on object and script will decrease move points. sasiad is array with objects next to that object. Where I made mistake?
void OnMouseDown()
{
if (see)
{
klikniety = true;
gameCtrl.GetComponent<GameControl>().move -= 1;
foreach (GameObject value in sasiad)
{
if (value != null)
value.GetComponent<PoleControl>().see = true;
}
gameObject.GetComponent<PoleControl>().enabled = false;
}
}
Answer by Bunny83 · Jan 26, 2016 at 08:54 PM
The "enabled" state of a MonoBehaviour script only controls the execution of some method which are actually bound to that state. This includes Awake, Start, Update, LateUpdate, FixedUpdate. Most event based callbacks still work.
Why is Late Update STILL WORKING when enabled??
Uhm I don't quite understand your question. Of course LateUpdate works if the script is enabled. Do you mean when it's disabled? No, Unity will NOT call LateUpdate when the script instance is disabled. Are you sure you don't call it manually somewhere? If you do you should never do this to any of the Unity callbacks because it leads to such bugs / misbehaviour.
If you have an actual issue you want an answer for, please ask a seperate question. Show your code and what you have done already to debug the issue. Have you placed a Debug.Log inside LateUpdate? Have you checked the stacktrace?
I understand that Awake still works when Script is disabled
Answer by brunocoimbra · Jan 26, 2016 at 09:38 PM
Just add an if statement:
void OnMouseDown()
{
if (!gameObject.GetComponent<PoleControl>().enabled)
{
return;
}
if (see)
{
klikniety = true;
gameCtrl.GetComponent<GameControl>().move -= 1;
foreach (GameObject value in sasiad)
{
if (value != null)
value.GetComponent<PoleControl>().see = true;
}
gameObject.GetComponent<PoleControl>().enabled = false;
}
}
FYI you can also just use "enabled" as that will refer to the enabled property of the current script component.
Answer by nikunjsth5 · Apr 15 at 02:54 AM
Never thought that OnCollision, OnTrigger and other functions work even if the script is disabled
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
.enabled Problems 1 Answer