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 JayFitz91 · Aug 16, 2014 at 11:26 AM · componentdisable

Problem disabling a script

I have a player that moves left to right when i touch on either side of my device. I also have it set up so that the clap animation plays when i touch him. The problem is, when i touch the player, the script for moving left and right is active and so he moves while he claps.

I would like to disable the movement script for the second i touch the player and enable it again after, but as it is at the moment the player still moves when i press on him.

Can anyone point out where I'm wrong? Code below:

 using UnityEngine;
 using System.Collections;
 
 public class clap : MonoBehaviour 
 {
 
     public LeftRightMovement leftRightScript;
 
     public AnimationClip clapHand;
 
     // Use this for initialization
     void Start () 
     {
         leftRightScript = GetComponent<LeftRightMovement>();
         animation.Stop ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetMouseButtonDown (0)) 
         {
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             RaycastHit hit;
             Debug.Log("hit");
             if(Physics.Raycast (ray, out hit) && hit.collider.gameObject.tag == "Player")
             {
                 Debug.Log("hit");
                 animation.Play ();
                 //The script i want to disable
                 leftRightScript.enabled = false;
 
             }
         }
         leftRightScript.enabled = true;
     }
 }

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 Ekta-Mehta-D · Aug 16, 2014 at 11:37 AM 0
Share

In Update , you have leftRightScript.enabled = true; .

So it will be always enabled.

1 Reply

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

Answer by Alessio89 · Aug 16, 2014 at 11:37 AM

Well, that's because you reEnable it soon after.

The thing is when you check for the mouse button down, you check only for one frame. That means that once i Click the mouse button, the next frame won't receive the same event, instead it will suppose that the mouse button hasn't been pressed this frame, thus reenabling the leftRightScript, even if i hold the button down. What you should do is either change GetMouseButtonDown with GetMouseButton, that way the script will stay disabled until you release the button, or check for GetMouseButtonUp and reenable the script once the button has been released.

Another solution would be to use a CoRoutine the length of the animation and then reenable the script:

     IEnumerator ReEnableLeftRightScript()
     {
      yield return new WaitForSeconds(animation["Clap"].length);
      leftRightScript.enabled = true;
     }
  void Update () 
     {
         if (Input.GetMouseButtonDown (0)) 
         {
  
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
             RaycastHit hit;
             Debug.Log("hit");
             if(Physics.Raycast (ray, out hit) && hit.collider.gameObject.tag == "Player")
             {
                 Debug.Log("hit");
                 animation.Play ();
                 //The script i want to disable
                 leftRightScript.enabled = false;
                 StartCoRoutine(ReEnableLeftRightScript());
             }
         }
       
     }

Comment
Add comment · Show 2 · 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 JayFitz91 · Aug 16, 2014 at 02:01 PM 0
Share

Thanks for the suggestion, that makes sense. I've just realised that I'm actually getting the error object reference not set to an instance of an object even with your method.

It is happening on your line 20 where i disable the other script. I was testing on android before so didn't see this.

Do you know what could be causing this?

avatar image Alessio89 · Aug 16, 2014 at 05:36 PM 0
Share

Uhm, that would be the "leftRightScript.enabled = true"? Am I right? (can't check at the moment). Anyway $$anonymous$$e was just a snippet, so can't really tell. It should be integrated with what you posted, ins$$anonymous$$d of replacing it :)

edit: check that leftRightScript is indeed assigned in your script, like you did in the Start() function

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

24 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

Related Questions

disable/enable script js 2 Answers

How to find a component and disable it from another objects script? 2 Answers

Unable to enable script 0 Answers

enable/disable specific components 3 Answers

How can I enable/disable an array of components? 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