- Home /
Disabling A Script on a GameObject From a Different Script
Hello.
I've been creating something recently which requires me to disable the "FPSInputController" component of the First Person Controller that comes with Unity.
I've implemented this bit of code which I thought would have worked:
GameObject.FindGameObjectWithTag ("Player").GetComponent<FPSInputController>().enabled = false;
However, Unity gives me an error when I try and use this:
The name "FPSInputController" does not exist in the current context.
I don't know why I cannot reference the FPSInputController on my Player. Can someone give me an idea?
Answer by Uncasid · Jun 16, 2014 at 10:23 PM
When you run your game, go to the editor before you run this piece of code. Check to see that the game object does in fact have a reference to the FPSInputController.
I might also suggest doing a singleton implementation if you plan on accessing it a bunch of times. Singletons with unity are pretty easy to implement:
public class MyClass : MonoBehavior {
public static MyClass myInstance = null;
void Start() {
myInstance = this;
}
}
Answer by hacky1892 · Jan 12, 2015 at 05:43 AM
I was the same problem, and I discovered that MonoDevelop IDE doesn't recognizes the issue. Just write your code, save it and Unity will not throw any error in the Console.
GameObject.Find("Player").GetComponent<FPSInputController>().enabled = false;
Try using Find() instead FindGameObjectWithTag()
Good look!