Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Disable a gameobject script with mouse down

Discussion in 'Editor & General Support' started by Nicolas_stone, Jan 16, 2022.

  1. Nicolas_stone


    Dec 16, 2017
    Hello, I am stuck on a problem.
    I want that when we hold the mouse button on a gameobjectA, it disables a C# script of a gameobjectB. And when we release the button, the C# script of the gameobjectB is activated again.

    Do you have any idea how I can do this? Thanks a lot.
  2. Kurt-Dekker


    Mar 16, 2013
    Yes, it's an awesome first place to start with Unity3D. What have you got so far?

    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.
    Screen Shot 2022-01-16 at 12.40.17 PM.png
  3. Nicolas_stone


    Dec 16, 2017
    thanks @Kurt-Dekker, I'm sorry I forgot to post my code but it doesn't work:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class detetc_mouseDown : MonoBehaviour
    6. {
    8.     void Update() {
    10.     if (Input.GetMouseButtonDown(0))
    11.     {
    12.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    13.         RaycastHit hit;
    14.         if (Physics.Raycast(ray, out hit))
    15.         {
    16.             if( =="montagne")
    17.             {
    18.                 Debug.Log(;
    19.             }
    20.         }
    22.     }
    26.     }
    29. }
  4. Kurt-Dekker


    Mar 16, 2013
    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: