Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by YanHoxley · May 17, 2015 at 07:59 PM · c#unity 5mouselookcastingenable and disable script

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... no problem

 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?

controller.jpg (27.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
 
 

?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YanHoxley · May 17, 2015 at 11:23 PM 0
Share

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

avatar image Bunny83 · May 18, 2015 at 12:23 AM 1
Share

@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;


avatar image YanHoxley · May 18, 2015 at 01:32 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Cram variables inside class, use inheritance, or something else? 1 Answer

Help...Android Shader Crash !!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges