- Home /
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;
}
}
In Update , you have leftRightScript.enabled = true; .
So it will be always enabled.
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());
}
}
}
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?
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
Follow this Question
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