Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can we disable a script on an object form other scripts ?

Discussion in 'Scripting' started by ksam2, Aug 21, 2013.

  1. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
    Hi, please somebody help me I have an object in my scene with a script on it. Now I want to disable that script from another script that isn't on the game object! How can I do that?

    I have following code for disable a script but how can I do that from another script that isn't on the object?

    Code (csharp):
    1. GetComponent(MoveForward).enabled = false;
    Please help thanks
     
    Last edited: Aug 21, 2013
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
  3. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    You could for example store the object you want to talk to in a variable in the 'other'-script.
    That way you will have a reference on the object and can easily change values.
     
  4. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
    I did it before but doesn't work
    Like this

    Code (csharp):
    1.  var slider1 : GameObject;
    2.  
    3.  slider2.GetComponent(MoveForward).enabled = false;
    But still doesn't work. do you know why?
     
    Last edited: Aug 21, 2013
  5. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,069
    Help
     
  6. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    did u drag the Object on the variable in the inspector?