- Home /
C# on/off any Script Component (mixed C# and JS)
Hey guys, I'm more familiar with JS... I can't get and enable/disable Script component in C#
This works (happy :) ), and CharacterController is switching On/Off as expected... 
 public GameObject mouselooking; // attached gameObject > FPSController in Inspector
 public bool isShowing;
 void Update() {
 if (Input.GetKeyDown("escape")) {
                 isShowing = !isShowing;
                 menu.SetActive(isShowing);
             if(isShowing==true) mouselooking.GetComponent<CharacterController>().enabled = false;
             if(isShowing==false) mouselooking.GetComponent<CharacterController>().enabled = true;
             }
         }
BUT how can I target another Component on the same GameObject, which is script First Person Controller (Script) in Inspector? I'm mad of this... this must be so easy and I can't manage that O.O Not even with google and my humble logic :D Thanx guys.
EDIT:
So... I somehow did it! BUT BY RANDOM TRYING WHOLE DAY and it not answered my question whatsoever!
This is solution for FPScontroller > First Person Controller script => Universal MouseLook locking in Unity 5
 public Behaviour FPSget; // attached gameObject > FPSController
 FPSget.enabled = false; // is switching Mouselook on-off
BUT it did not solve my question: How can I disable / enable ANY script component under same GameObject in Unity5? For example Crosshair.js? How I call its name and disable it?
Answer by Cherno · May 17, 2015 at 10:37 PM
Uhmmm.. you just exchange
  if(isShowing==true) mouselooking.GetComponent<CharacterController>().enabled = false;
          if(isShowing==false) mouselooking.GetComponent<CharacterController>().enabled = true;
with
 if(isShowing==true) mouselooking.GetComponent<First Person Controller>().enabled = false;
          if(isShowing==false) mouselooking.GetComponent<First Person Controller>().enabled = true;
 
 
?
sorry Cherno this was 1st I was trying ;) Tried like 50 diff ways how to archieve that. Nothing. I am using Unity5
 GetComponent<First Person Controller> = Error: Unexpected symbol `Person'
 GetComponent<FirstPersonController> = Error: The type or namespace name `FirstPersonController' could not be found. Are you missing a using directive or an assembly reference?
 GetComponent<Crosshair> = same error
even old tutorials over web says this is solution, but it seems stopped work in Unity 5 (Personal), it keeps saying <whatever> whatever not exist, its works only if is equal to charactercontroller or light or some type but NOT SCRIPT itself
@YanHoxley: I guess you try to mix UnityScript (Unity's javascript) with C#, right? That's the actual problem. Interactions between multiple languages are very limited as each language is compiled seperately.
So the best solution is: Don't mix languages if you can avoid it. If you can't you only have a few options:
- Use a public variable of a common base type that is available in both languages and that provides the thing you want to change. This is what you already have found out yourself. You can declare a variable of tyoe $$anonymous$$onoBehaviour (or Behaviour which is the base class or $$anonymous$$onoBehaviour) and then you can enable / disable the script. 
- At runtime simply use the string verstion of GetComponent("FirstPersonController"). However the string version returns a reference of type "Component" as at compile time it can't know what type it will return. You have to cast the type into $$anonymous$$onoBehaviour / Behaviour to be able to access the enabled property. 
Example:
 $$anonymous$$onoBehaviour script = ($$anonymous$$onoBehaviour)GetComponent("FirstPersonController");
 script.enabled = false;
@Bunny83 thank you so much, I get it now, coz I've tried to put empty C# and it really worked like mentioned in tutorials, @Cherno and so. You have moved me further.
- Well, I really want to avoid C#, I don't like it. As a web developer and so, I really like UnityJS and can do game logic easily and plan and understand what I'm doin. But it's not always possible (as a Unity beginner). Some standard assets are only in C# and also some scripts I need for learning purposes. I don't want to do this bypass you wrote so often, but sometimes it's only way to do for me now. 
Anyway I'm still lost :) Could you help me to deter$$anonymous$$e final solution I could learn from? I feel dumb lol, tried put it together by myself to not bother anymore, but failed a bunch... Still can't address to that JS. I commented in code and cleaned my funny tryouts :).
 public class showUI : $$anonymous$$onoBehaviour {
     public GameObject menu; // << just Canvas
     public GameObject mouselooking; // << this is object where Crosshair.js is Component
     public Behaviour FPSget; // getting FPS controller behaviour on/off (this works already)
     public bool isShowing;
     // I think `script' must be declared here somehow??
 
     void Start() {
         Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
         Cursor.visible = false;
         $$anonymous$$onoBehaviour script = ($$anonymous$$onoBehaviour)GetComponent("Crosshair"); 
         //crosshair.js (I can do crosshair in C# for sure, it's just example JS script)
         // gives warning CS0219: The variable `script' is assigned but its value is never used
     }
     void Update() {
             if (Input.Get$$anonymous$$eyDown("escape")) {
                 isShowing = !isShowing;
                 menu.SetActive(isShowing);
             if(isShowing==true) {
                 mouselooking.GetComponent<CharacterController>().enabled = false;
                 //mouselooking.GetComponent<testdisabling>().enabled = false; //C# script works
                 Cursor.lockState = CursorLock$$anonymous$$ode.None;
                 Cursor.visible = true;
                 FPSget.enabled = false;
                 script.enabled = false; //gives error CS0103: The name `script' does not exist in the current context
             }
             if(isShowing==false) {
                 mouselooking.GetComponent<CharacterController>().enabled = true;
                 Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
                 Cursor.visible = false;
                 FPSget.enabled = true;
                 script.enabled = true; //gives error CS0103: The name `script' does not exist in the current context
             }
             }
         }
     }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity 5 access First Person Controller Mouse Look Sensitivity via script 3 Answers
Serializable class definition in base class doesn't show in editor 1 Answer
Updating Unity 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                