Enabling/Disabling a script programatically

Hello everyone,

Boy am I glad to get to know Unity and it rapidly developing technology.

This is my first post, so Hi.

I wanted to ask, can i enable disable scripts programatically?

Here’s what i wanna do:
1- I created a GUI which can be shown/hidden by presseing M button.

2- I want the mouselook script of my main camera to be disabled.

the 1 was super easy and I did it using getkeydown and up. And for the 2 one I included extra code in my MouseLook script of the camera.

But I don’t like that. I want to control the mouse look script from another script file.
Is that possible?

Thanks a million.

Unity Rocks so far,

Yup! If you store a reference to the script like this: var myLook : MouseLook; You can then put a boolean inside the script that turns it on and off like var switch : boolean = true;. After that, you’d just need to say myLook.switch = false; to turn it off and myLook.switch = true; to turn it on again.

Like all variables, the above myLook would have to have the object with the MouseLook script dragged onto it.

hey again,

thanks for the reply,

Could you help reference the mouselook in C#?

I have a cs script file names “guimenu”
and inside its monobehaviour class i wrote:

public MouseLook ml;

and also"

if M key is pressed 
      then trigger ml.AllowMouseMovement value.

and then in my MouseLook script i put a bool value:
bool AllowMouseMovement=true;

But it is not working.
Should i do somthing else?

oh and I made my MouseLook ml public and I dragged the mouselook script of that camera to the variable of my gui control thingy.

function Update()
{
  if (AllowMouseMovement) {
    // MouseLook Code here
  }
}

The above should be how it works. Also, your toggle should be m1.AllowMouseMovement = !m1.AllowMouseMovement;

Actually that’s how i wrote it.

And I had to include two MouseLook objects because the FirstPersonController keeps the mouseX and mouseY seperately.

And I refreshed everything and it works!!!

Still very easy to do.

Hmmmm… what if I want to enable disable blurry effect of camera?

What should i do with OnRenderImage?

/////
Ok I found the answer:

BlurEffect effect1;

then in my code:
effect1.enabled=!effect1.enabled;

hello Aram

i try myself to open close a GUI Window but i cant
have it to stay in the screen
can you help me with the getkeydown and up to understand the way for enabled my inventory in gui

thanks for your answer

Francois

All Script Components have an “enabled” boolean.
Unity will not run them if you set enabled = false.

So simply create a reference to your script:

var mouseLook : MouseLook;

Set it to the correct instance in the unity editor and then set:

mouseLook.enabled = false;

when you wish to turn it off.
Your own scripts have this ability also as it is inherited throughout the Unity engine.

when i type:

var mouseLook : MouseLook;

it says: The name ‘MouseLook’ does not denote a valid type.

help

I had the same issue with the water scripts.
I think it is an issue with calling c# scripts in javascript.

This is covered here in the manual.

None of that makes any difference in my case.
It simply will not recognise the pro assest water scripts as a valid type in a javascript file.

It does. The page just doesn’t explain clearly that this compilation ordering will solve your problem ;). It would probably help a lot of people if that manual page would explicitly mention the ‘does not denote a valid type’ error in the first paragraph.

For someone like me that is a Newbee, can you please post how this script would look. I am currently having issues with stopping camera controller once I make the switch. I am currently using the code from this site http://www.unifycommunity.com/wiki/index.php?title=SwitchCamera. But it doesn’t tell me how to disable the Mouselook or Thirdperson controller from the camera as I make the switch. If you can help me out with this, I would Greatly appreciate it :slight_smile:

I use this:

Camera.main.GetComponent("MouseLook").enabled = false;
var other =  GameObject.Find("First Person Controller");
other.GetComponent("MouseLook");
var otherScript =other.GetComponent("MouseLook");
otherScript.enabled = false;

but its still not perfect.
And I havent figured how to enable it again.

I was told to use

function Awake()

{this.enabled = true; // or false
}

but I dont know how to use it…