- Home /
Disable all instances of a component
I am trying to disable mouse look on 'q'.
Here is my code as is.
#pragma strict
//Variables
public var paused = false;;
//This script is on the main camera
function Start () {
}
function Update () {
if(Input.GetKeyDown("q")){
paused = !paused;
}
if(paused){
//Toggle caputred cursor
Screen.lockCursor = false;
//disable mouselook
//This needs to be changed to disable the FPS controllers script
GetComponent(MouseLook).enabled = false;
//This should kill the cameras script
Camera.mainCamera.GetComponent(MouseLook).enabled = false;
Time.timeScale = 0;
}
if(!paused){
Screen.lockCursor = true;
GetComponent(MouseLook).enabled = true;
Camera.mainCamera.GetComponent(MouseLook).enabled = true;
Time.timeScale = 1;
}
}
The issue is that it only disables it on one axis (e.g cameras axis or players axis ).
So how do i disable both scripts and re-enable both scripts on the generic First Person Controller?
-Grefuntor
What "both" scripts? I see you only trying to access $$anonymous$$ouseLook.
I also would recommend not referencing scripts during execution. You should only need to access them once at the start of the scene.
Create a variable such as LookFor$$anonymous$$ouse and assign it to $$anonymous$$ouseLook.
var $$anonymous$$ouseLook : LookFor$$anonymous$$ouse;
Then place LookFor$$anonymous$$ouse = GetComponent("$$anonymous$$ouseLook"); within the Awake(). The script will be available the rest of the time during game play. If you call for the component everytime you use those if statements, it will hurt your games performance.
Then within your if statements, activate or deactivate them through LookFor$$anonymous$$ouse.enabled = true or false;
What exactly; is it doing right now? Are you getting any errors?
Right now it does work. But it only stops me from looking up and down. I need to disable mouselook, but the default set-up has 2 mouselook scripts for some reason, and i need to stop them both.
Edit: let me clarify that- $$anonymous$$ouse look is the component i want to stop, but there seems to be more than one on the player.
@GC83: you shouldn't use quotes in GetComponent, but it doesn't really make any difference whether you cache the call if you're just doing it occasionally. Also the convention is to use lowercase for variables (uppercase for functions and classes).
To each their own. But, I suppose it would help for clarifying variable types.
Answer by DaveA · May 07, 2012 at 02:30 AM
I'm not sure why you would want or need 2 MouseLooks on one object, but you could use GetComponents(MouseLook) to get a list of them, then use a 'for' loop to set each's enables as needed.
I didn't set it like that... It's just the standard First person controller. Will try the for loop.
The FPS has one $$anonymous$$ouseLook on the 'body' and one on the camera ('head') ... I assume it's because when looking up/down it 'tilts', so if this were on the body of the controller, it would be upsetting it's stance/balance. $$anonymous$$inda like if your neck, spine and knees were locked and you could only look up/down by pivoting on your ankles.
$$anonymous$$akes (some) sense. How should I go about disabling this? I'm not sure of how I should be doing this. I tried GC83's suggestion, but 'var $$anonymous$$ouseLook : LookFor$$anonymous$$ouse;' threw an error.
Sorry, backwards. LookFor$$anonymous$$ouse : $$anonymous$$ouseLook;
@Grefuntor: do what Dave A said, although you would need GetComponentsInChildren rather than GetComponents. The docs even have a code example doing exactly what you want.
Answer by LauraCortes · Dec 13, 2012 at 08:18 PM
http://answers.unity3d.com/questions/53917/mouse-look-and-character-motor-off-but-i-can-still.html I think haxard's answer is what you need.