Jump to content

How to enable/disable a PartModule in code


Recommended Posts

I'm trying to write a module that will enable or disable another PartModule in code. I've found a number of likely looking candidates and tried each of them without success. If anyone can point me in the right direction I would greatly appreciate it.

So far this is what I've tried:

I feed in the module index (like the wheel modules do) and have verified it's accessing the correct module (ModuleSAS in this case)

		// Get the modules on the part in OnStart
       PartModule[] modules = GetComponentsInParent<PartModule>();
		// Save a reference to the module so we don't have to find it each time
       targetModule = modules[targetModuleIndex];
		// And one by one I tried the following
       targetModule.isEnabled = false;
       targetModule.moduleIsEnabled = false;
       targetModule.enabled = false;
       targetModule.part.State = PartStates.DEACTIVATED;
       targetModule.part.enabled = false;
       targetModule.part.deactivate();

I've tried fetching the module list each time and directly setting the fields instead of storing it in targetModule:   modules[targetModuleIndex].isEnabled = false;

I've verified that when I set one of the enabled/state fields that it remains set when I reload the module list.

Instead of disabling the component I've used RemoveComponent to completely remove the component – the module is back when I reload the list.

In all cases the toggle SAS button remains in the right click menu and I'm able to toggle the SAS between active and inactive despite setting the various enables/states to off.

Any insights/clues/suggestions appreciated.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...