Is it expected behavior to have a method of a component be triggered via a broadcast call, even when the component resides in a gameobject set to disabled? I thought if it was disabled it wouldn’t receive these events. Is this a bug, or a feature, and if the latter is there a workaround, or do I need to hack something on my own?
4.2.0f4 version of unity, non pro.
Thanks
Behaviour you need can be obtained using gameObject.active
Thanks Damian, that seems like a workaround right? As it stands I have one game object with several behavior components I toggle on/off based on state. It seems only logical to me that enable/disable should work in this instance, and if a component is disabled it shouldn’t receive any events, including a broadcast. Can anyone confirm if this is a bug or intend behavior? I hate to have to put my components into subobjects to get this functionality, seems silly. I wonder how many other places I’ve not discovered yet are working incorrectly because of this, or is it a new problem in 4.x that I didn’t catch in earlier testing because it wasn’t there?
I think what he meant was are you using gameObject.setActive(false)? Gameobjects are set active and inactive, components are enabled and disabled.
I understand gameobject.setactive(xx) vs component.enabled=xx that’s not the question
I have a single game object with several components. I am currently enabling various components within the object. I don’t want to deactivate the parent object and it seems logical that if I disable a component it shouldn’t receive events, but I have a situation where a disabled object is in fact receiving events. The parent game object is still active, and I want it to stay that way.
I realize as a workaround I can probably put the components I’m toggling on/off into a sub gameobject and instead active/deactive the game object containing the component, but that seems ugly and I question is that the right way to do it, or should component.enabled=false not receive events, and the fact it is receiving events is a bug?
So, you are saying you disabled a script and you can still call functions from it and the update is still running?
Did a quick search and found this:
Disabling a script only turns off Update (plus related such as FixedUpdate) and OnGUI, so if those functions aren’t present then disabling a script isn’t possible.
So, not really a bug, just the way they do it. Those are the real hogs for memory etc.
So update and fixedupdate are correctly suspending when I set enabled=false but when in a separate, active component I perform a Broadcast and the method I’m calling exists in the disabled component, enabled=false, its still running the method and failing because variables arent properly initialized because I dont expect it to run since its disabled. I guess I need ti add code to the method to see if its in a disabled state but this seems dumb and inefficient to me.
I agree. I’d rather the whole thing was disabled. I’m glad you brought up this topic because it would be a debugging nightmare if I weren’t aware of it. I’ve just taken to disabling scripts to save memory. I think once I started doing it much, I probably would have added a test anyway. I’ll probably just mark scripts that I enable/disable. I mainly do it now if I consider it a hog, like OnGUI. If it’s an object out of view, I would just make it inactive.
For anyone in the future that comes across this post and wants an easy fix, I added this to the beginning of any method that can be called via a Broadcast:
if ( this.enabled == false )
return;
Works great now. IMHO, we shouldn’t need this. If the component is set to enabled=false, then issuing a broadcast shouldn’t be received, but until such time that Unity developers agree, this should serve as a viable workaround.
Thanks to all the discussion to those that replied.
Larry