Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Digital-Phantom · Jan 10, 2015 at 05:00 PM · javascriptcomponentcharacter controllerenabled

How do I disable Script on FP Controller? (Solved)

I'm trying to disable the 'CharacterMotor' script on the First Person Controller when I change between cameras in my scene.

I'm using this script - var cam1 : Camera; var cam2 : Camera;

 function Start()
 {
     cam1.enabled = true; cam2.enabled = false;
 }
 
 function Update()
 {
     var myScript = GameObject.Find("FirstPersonController").GetComponent("CharacterMotor") ;
     
     if (Input.GetKeyDown(KeyCode.C))
     {
            cam1.enabled = !cam1.enabled; myScript.enabled = false;
            cam2.enabled = !cam2.enabled; myScript.enabled = true;
      }
 }

And getting this error -

NullReferenceException: Object reference not set to an instance of an object SwitchCameras.Update () (at Assets/Scripts/SwitchCameras.js:11)

What am I doing wrong? As usual any help is greatly appreciated.

(alternatively if there is a way to switch off/disable the character controller itself temporarily until I switch back to it, that would also be great)

???

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Digital-Phantom · Jan 11, 2015 at 05:24 AM

Sorted ! Just switched the Boolean and getkey parts round and works fine

 function Update()
 {
     var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");
 
     if (cam2Active == false)
         {
             if(Input.GetKeyDown(KeyCode.C))
             {
                 cam1.enabled = false;
                 cam2.enabled = true;
                 freezePlayer.enabled = false;
                 cam2Active = true;
             }
         }
     
     else    
         
     if (cam2Active == true)
         {
             if(Input.GetKeyDown(KeyCode.C))
             {
                 cam1.enabled = true;
                 cam2.enabled = false;
                 freezePlayer.enabled = true;
                 cam2Active = false;
             }
         }
 }

Just thought I'd put the answer up in case it might help anybody else in future !

Comment
Add comment · 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
0

Answer by Mmmpies · Jan 10, 2015 at 05:08 PM

Call your cameras Cam1 and Cam2 then in Start() before you enable/disable them do this:

 cam1 = GameObject.Find("Cam1");
 cam2 = GameObject.Find("Cam2");

My JS is poor though so may need tweaking.

EDIT:

Also shouldn't you have an if statement for checking the cam1.enabled? like I said not well up on javascript but it looks like you should have:

 if(cam1.enabled)
 {
     // do your code here
 }
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 Digital-Phantom · Jan 10, 2015 at 05:18 PM 0
Share

The camera switching works fine, its the actual FirstPersonController component I want to access and enable/disable

avatar image Mmmpies · Jan 10, 2015 at 05:23 PM 1
Share

Ah sorry, working in java never sits well with me. How about adding a Debug.Log to show if myScript has been picked up?

 Debug.Log("myScript = " + myScript);

just after you set it.

EDIT:

And I'm being an idiot again, looks like you're searching for a FirstPersonController and not the Player or enemy.

Search for the name of the character that the character motor and and first person controller is on then GetComponent("Character$$anonymous$$otor")

EDIT2:

Really should make that clearer, both FirstPersonController and Character$$anonymous$$otor are components that generally go on a Player GameObject.

So if you have a Player GameObject that these are on then you reference them with:

 var myScript = GameObject.Find("Player").GetComponent("Character$$anonymous$$otor");
 
 //or if using Tags
 
 var myScript = GameObject.FindGameObjectWithTag("Player").GetComponent("Character$$anonymous$$otor");

Unless you've called your Player FirstPersonController (which is a bad idea).

avatar image Digital-Phantom · Jan 11, 2015 at 05:07 AM 0
Share

O$$anonymous$$ I've decided to re-write the script. Still doing what I want but hopefully making it easier to follow (and add to later)

 var cam1 : Camera;
 var cam2 : Camera;
 var cam2Active : boolean = false;
 
 function Start()
 {
     cam1.enabled = true;
     cam2.enabled = false;
 }
 
 function Update()
 {
     var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("Character$$anonymous$$otor");
 
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
         {
             if(cam2Active == false)
             {
                 cam1.enabled = false;
                 cam2.enabled = true;
                 freezePlayer.enabled = false;
                 cam2Active = true;
             }
         }
     
     else    
         
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
         {
             if(cam2Active == true)
             {
                 cam1.enabled = true;
                 cam2.enabled = false;
                 freezePlayer.enabled = true;
                 cam2Active = false;
             }
         }
 }


$$anonymous$$y problem now is that the main camera won't re-enable? I'm fairly sure its to do with the 'cam2Active' boolean not being recognised by the second IF statement. I know I need to add it to the IF statement, just not sure where (tried a couple of places with no luck)

any suggestions ???

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

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BCE0019: 'enabled' is not a member of 'UnityEngine.Component'. ??? 2 Answers

Convert this line of javascript to C# (easy) 1 Answer

Disable all components but 1 on a gameobject 1 Answer

Using Mechanim for FP Animation - Help 0 Answers

player prefs, problem but no problem??? 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