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
1
Question by Jabes22 · May 18, 2013 at 05:06 PM · disablecontrols

How can I disable controls?

I know this question has been asked, but I can't get it to fire right. I've read a few solutions, but can't seem to work it out. I'm trying to cut controls for cinematic scenes with the first person controller.

Right now I have a wonky set up I think..

I have a script on both the camera and the player controller.

Camera Script

 #pragma strict
 
 function EnableControls(){
 
     var CamControls = GetComponent.<MouseLook>();
     CamControls.enabled = CamControls.enabled;
 
     var PlayerControls: GameObject;
     
     PlayerControls.GetComponent(DisablePlayerControls).EnablePlayerControls();
 
 }
 
 function Start () {
 
     var CamControls = GetComponent.<MouseLook>();
     CamControls.enabled = !CamControls.enabled;
 
 }
 
 function Update () {
 
 }

The script on the Player controll

 #pragma strict
 
 function EnablePlayerControls(){
 
 var MouseLook = GetComponent.<MouseLook>();
 var Motor = GetComponent.<CharacterMotor>();
 var Controller = GetComponent.<FPSInputController>();
 
 MouseLook.enabled = MouseLook.enabled;
 Motor.enabled = Motor.enabled;
 Controller.enabled = Controller.enabled;
 
 }
 
 function Start () {
 
 var MouseLook = GetComponent.<MouseLook>();
 var Motor = GetComponent.<CharacterMotor>();
 var Controller = GetComponent.<FPSInputController>();
 
 MouseLook.enabled = !MouseLook.enabled;
 Motor.enabled = !Motor.enabled;
 Controller.enabled = ! Controller.enabled;
 }
 
 function Update () {
 
 }

I created an animation on the main camera using the built in animation feature. I try to fire an event from the animation which is a function from an instance of the script on the player control.

Errors I get are..

  1. NullReferenceException: Object reference not set to an instance of an object DisableCameraControls.EnableControls () (at Assets/Scripts/MenuScripts/DisableCameraControls.js:12)

  2. AnimationEvent 'EnableCameraControls' has no receiver!

Thanks in advanced to anyone who can help me sort this out. Thanks guy :)

Comment
Add comment · Show 1
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 randomuser · May 18, 2013 at 05:20 PM 0
Share

The lazy way to do this temporarily is to add a variable to see whether the animation is playing and if don't let the movement code run. It's not a permanent as deleting and re adding the game object, but could work while you are waiting for a solution

1 Reply

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

Answer by aldonaletto · May 18, 2013 at 05:21 PM

In order to disable controls in the First Person Controller prefab, a better alternative is to get all scripts of interest at Start and just enable/disable them when needed - like this:

 #pragma strict

 private var MLook1: MouseLook;
 private var MLook2: MouseLook;
 private var ChMotor: CharacterMotor;
 
 function Start(){
   MLook1 = GetComponent(MouseLook);
   MLook2 = Camera.main.GetComponent(MouseLook);
   ChMotor = GetComponent(CharacterMotor);
 }
 
 function EnablePlayerControls(enable: boolean){
   MLook1.enabled = enable;
   MLook2.enabled = enable;
   ChMotor.enabled = enable;
 }

Notice that you don't need a camera specific script, since this one does the job - just call EnablePlayerControls(true) to enable the controls, or pass false to disable them. You don't need either to disable FPSInputController - this script just passes info to CharacterMotor. Actually, you may have two different behaviours: disabling FPSInputController disables control but the character finishes its current movement - if it's falling, for instance, it will stop only when reaching ground. Disabling CharacterMotor, on the other hand, will stop the character immediately - it may get frozen in the middle of a fall, in the example above.

NOTE:Don't use variables with the same name of scripts or other components - like MouseLook, for instance. The variable hides the original class (script or component) and you get lots of errors or warnings.

Comment
Add comment · Show 10 · 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 randomuser · May 18, 2013 at 05:22 PM 0
Share

The variable masking is a common problem.

avatar image aldonaletto · May 18, 2013 at 05:36 PM 0
Share

Yes, and most programmers forget the damned variable and almost get crazy thinking that Unity is defective. To make things worse, usually the hidden component is a very popular one, like Rigidbody: Unity suddenly starts reporting that AddForce isn't part of Rigidbody, and that sort of things.

avatar image Jabes22 · May 18, 2013 at 05:37 PM 0
Share

hmm... I'm not sure this will work in my instance... The thing is I need to enable and disable controls on command. I need to be able to do it for every cut scene. I need a script on the camera to control this because the fire event feature for animation only works with script attached the the camera...

I noticed a major problem in my code before, so I've been fixing it up a little...

Here's the Player script code:

 #pragma strict
     
     // This function will Disable Player Controls
     
     function DisablePlayerControls(){
     
         var $$anonymous$$ouseLook = GetComponent.<$$anonymous$$ouseLook>(); // variable for mouse look of player
         var $$anonymous$$otor = GetComponent.<Character$$anonymous$$otor>(); // variable for character motor of player
         var Controller = GetComponent.<FPSInputController>(); // variable for controller of player
     
         $$anonymous$$ouseLook.enabled = !$$anonymous$$ouseLook.enabled; // sets mouse look of player to not enabled
         $$anonymous$$otor.enabled = !$$anonymous$$otor.enabled;  // sets motor of player to not enabled
         Controller.enabled = ! Controller.enabled; // sets controller of player to not enabled
     
     
     }
     
     // This function will enable Player controlls.
     
     function EnablePlayerControls(){
     
         var $$anonymous$$ouseLook = GetComponent.<$$anonymous$$ouseLook>(); // variable for mouse look
         var $$anonymous$$otor = GetComponent.<Character$$anonymous$$otor>(); // variable for character motor
         var Controller = GetComponent.<FPSInputController>(); // variable for controller
     
         $$anonymous$$ouseLook.enabled = $$anonymous$$ouseLook.enabled; // set mouse look of player to enabled
         $$anonymous$$otor.enabled = $$anonymous$$otor.enabled; // set motor of player to enabled
         Controller.enabled = Controller.enabled; // set controller of player to enabled
     
     }
     
     function Start () {
     
     
     
     }
     
     function Update () {
     
     }

Here's the camera script code

 #pragma strict
 
 // This function will disable all controls (Add call to Disable player controls from script Disable Player Controls
 
 function DisableAllControls(){
 
     var CamControls = GetComponent.(); // creats variable for mouse look of camera
     CamControls.enabled = !CamControls.enabled; // Disables the mouse look.
     
     
 
 }
 
 // This function will enable all controls cam/player add call to enable controls from disableplayer controls.
 
 function EnableAllControls(){
 
     var CamControls = GetComponent.(); // creates a variable to hold $$anonymous$$ouselook component of camera
     CamControls.enabled = CamControls.enabled; // Sets $$anonymous$$ouseLook to enabled.
 
 
     
 }
 
 function Start () {
 
     
 
     
 
 }
 
 function Update () {
 
 
 
 }

Notice that I have two functions in the camera script code that I'll call from the animations to disable and enable controls. The problem is I don't know how to access functions from my player script to get them to fire in the camera script...

Sorry if there's a better way... let me know. I'm not the most logical guy runnin, lol.

avatar image Jabes22 · May 18, 2013 at 05:45 PM 0
Share

wait, wait. I never thought of functions with arguments. Your method will work better with one script. I just need a script on my camera with one function called controls that takes a boolean like you said...

I just need to know how to access the components of another game object properly...

can you help with that?

avatar image Jabes22 · May 18, 2013 at 05:50 PM 0
Share

So if I have one script on the camera like so

 #pragma strict
 
 // This function will handle enable and disable of all controls.
 
 function AllControls(var Enabled : boolean){
 
     if(!Enabled){
     
     
     
     var CamControls = GetComponent.<$$anonymous$$ouseLook>(); // creats variable for mouse look of camera
     CamControls.enabled = !CamControls.enabled; // Disables the mouse look.
     
     }else{
     
     var CamControls = GetComponent.<$$anonymous$$ouseLook>(); // creats variable for mouse look of camera
     CamControls.enabled = CamControls.enabled; // Disables the mouse look.
     
     
     }
 
 
 
 function Start () {
 
     
 
     
 
 }


How can I access the player controls on the FPC in this script and add them to enable and disable. I have a bit of a problem with that. I'm not really sure how to get into other objects scripts and components very well...

 function Update () {
 
 
 
 }
Show more comments

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

15 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

Related Questions

How to get a rigidbody to move left and right? 1 Answer

Disable/Enable Colliders 1 Answer

Recommended way to delay controls momentarily? 2 Answers

How to disable collision2D damage when I am not attacking? 0 Answers

Player or character select 2 Answers


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