- Home /
How to disable a custom script in a custom GameObject with C# ?
Hello! So, I have a Player GameObject, with a "FirstPersonController" script attached as component, ok?
Now, what I'm trying to do, is that when I enter into the Main Menu, the script simply turn off, so i'm unable to use "MouseLook" inside the "FirstPersonController" while i'm in the menu, got it?
Hope that is clear. I just tried the "Time.timeScale = 0;" statement, but it blocks only the movements. I need to turn off the script completely, and reactivate it when I restore the game.
Thank you.
PS= I use Unity5 and I'm not using JS.
I'm not sure how this comment is not useful considering I provided you with the necessary features to do what you asked for? I also pointed out that it's pretty easy to figure this out by doing a little bit of google search, which in turn is also one of the requirements for posting a question on this website.
Answer by DoTA_KAMIKADzE · May 12, 2015 at 05:29 PM
You have 3 general ways to enable/disable functionaliy of your script:
1)Add/Remove script when you need. I don't think that this is what you expect so I'll omit it.
2)Enable/Disable script.
//when you need to disable:
GetComponent<FirstPersonController>().enabled = false;
//then when you need to enable:
GetComponent<FirstPersonController>().enabled = true;
//if you will do that from another script you'll need to get reference to your FirstPersonController script
GameObject gObj = YourGameObjectThatHoldsFPC;
gObj.GetComponent<FirstPersonController>().enabled = false;
Check out my answer THERE if you have no idea how to get the reference to your script.
3)Use some restriction variable, like for example boolean. This way only the part of code you don't need will not execute.
//Just set it true false when you need and wrap your code that shouldn't execute in if(yourBool){}
GetComponent<FirstPersonController>().yourBool = false;
You'll need to get the reference to script with this code as well if you'll change this bool from the other code.
Also note that you shouldn't write your code like that above^ it was just for example, as instead store reference to your script once and then use that variable, e.g.:
FirstPersonController yourController;
//then use it like:
yourController.enabled = true;
I already tried a lot of times these statements, but they don't work. Unity says to me that can't find the "FirstPersonController" script.
I just found a temporary solution anyway, that works, that consist in adding these "" before and after the name of the script, and adding at the end "as behaviour".
Honestly I really don't know why your code doesn't work to me, i saw the same code in a lot of other websites.. bah! :(
Could you post the actual error along with script?
P.S. Specifically I'd like to see your FirstPersonController class declaration (e.g. the line of code that contains "class FirstPersonController") and the lines of code where you use GetComponent.
Also make sure that FirstPersonController is actually attached to object from which you try to get it (except if you utilize FindObject(s)OfType, in that case it would be pretty straight forward).
So, the declaration code of "FirstPersonController" is this one:
**public class FirstPersonController : $$anonymous$$onoBehaviour
{ ... }**
The statement that I used (and that doesn't work for me) is this one:
**GameOBJ.GetComponent<FirstPersonController>().enabled = false;**
And the error I get from the code below, is this one:
**Assets/I$$anonymous$$7 Scripts/CentralTrigger.cs(32,46): error CS0246: The type or namespace name `FirstPersonController' could not be found. Are you missing a using directive or an assembly reference?**
$$anonymous$$y temporary solution that I found, is this one:
(GameOBJ.GetComponent("FirstPersonController") as $$anonymous$$onoBehaviour).enabled = false;
Can you explain me why the first code doesn't work like the second? I can assure you that the script is correctly attached on the object, and has the checkbox.
Well for some reason Unity can't locate your FirstPersonController script.
$$anonymous$$ake sure that your FirstPersonController script is located inside Assets folder + that it is not hidden by Unity's magic folders + your folder containing this script should not start with dot (.folder).
Double check na$$anonymous$$g of FirstPersonController scrip and FirstPersonController class name and used FirstPersonController name inside the script where you call GetComponent - all of those should be with equal name.
Answer by soapOD · Jan 19, 2017 at 10:35 PM
Just in case someone in the future needs an answer to this...
You need to add the appropriate namespace to reference the first person controller.
Add this to the top of your script:
using UnityStandardAssets.Characters.FirstPerson;
then you can reference the first person controller by using: player.GetComponent<FirstPersonController>().enabled = true;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GUIText component won't render 1 Answer
Textures changing for no reason 0 Answers
How to loop an array of 2d sprites 1 Answer