Search Unity

How to disable c# script on Go from a javascript

Discussion in 'Scripting' started by Krodil, Aug 7, 2012.

  1. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Hey,
    Ive now spent two days trying to figure this out.
    I have a gameobject with a jscript on which functions as a toggle.
    On another gameobject I have several scripts, where one of them is a c# script. When something is 'true' it must be enabled and vice versa.
    I have tried accessing it with standard GetComponent, but all it does is deactivating the Go, not the script.
    And I am told that .active is obsolete.

    Code (csharp):
    1. function Update ()
    2. {
    3.     if(objectMode == false)
    4.     {
    5.         //GameObject.Find("_PinchRotationSample").active = true;
    6.         //var test = Camera.main.GetComponent("TBDragOrbit");
    7.         //var test = GameObject.FindWithTag("ObjectModeTAG");
    8.         //print(test);
    9.         //test.active = false;
    10.         //test.GetComponent("PinchRotationSample").active = false;
    11.         //test.active = false;
    12.     }
    13.     if(objectMode == true)
    14.     {
    15.         //var test1 = gameObject.FindWithTag("ObjectModeTAG");
    16.        
    17.         //var test1 = Camera.main.GetComponent("TBDragOrbit");
    18.         //((Behaviour)("PinchRotationSample")).enabled = true;
    19.        
    20.         //GameObject.FindWithTag("ObjectModeTAG").GetComponent("PinchRotationSample").active = false;
    21.        
    22.         //test1.active = true;
    23.     }
    24. }
    Above are some of my attempts.

    I must also add that I also have a jscript on the object, this is not disabled either..still only the Gameobject.

    I then added a tag to the mentioned gameobject and tried this:

    Code (csharp):
    1. GameObject.FindWithTag("ObjectModeTAG").GetComponent("PickObject").active = false;
    "PickObject" is a jscript on the gameobject where I wish to disable scripts.
    still same result.

    Hope to get some pointers from you
    Best,
    Kro
     
  2. Jean-Moreno

    Jean-Moreno

    Joined:
    Jul 23, 2012
    Posts:
    593
    You might want to look at the .enabled property; i.e. changing your code to:
    Code (csharp):
    1. GameObject.FindWithTag("ObjectModeTAG").GetComponent("PickObject").enabled = false;
    According to the Reference, .active is for the GameObject class, .enabled is for the Behaviour class (including MonoBehaviour scripts)