Search Unity

Enable/Disable script from another script (JavaScript)

Discussion in 'Scripting' started by Wolves_Realm, Jan 20, 2012.

  1. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    Hi,
    There are two scripts attached to my object. I want to disable the second script (which is called fireScript) from the first script.
    I used the following code to do that:
    Code (csharp):
    1.  
    2. gameObject.GetComponent("fireScript").enabled = false;
    3.  
    I get this error:
    'enabled' is not a member of 'UnityEngine.Component'

    I googled for 3 hours to find the solution, some had suggested some solution such as
    Code (csharp):
    1.  
    2. gameObject.GetComponent<fireScript>().enabled = false;  // I'm not even sure if it is a Javascript!! Anyways it didn't work.
    3.  
    4. GetComponent("fireScript").enabled = false; // without using gameObject before GetComponent, yet didn't work!
    5.  

    I am using Unity 3.5.0b6 free version. Could this be a problem of Beta version of Unity?
    Any suggestions?
     
  2. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Try this (C#):

    Code (csharp):
    1.  
    2. ((fireScript)gameObject.GetComponent<fireScript>()).enabled = false;
    3.  
     
    LordNickBDOF likes this.
  3. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    Are the two scripts attached to the same game object? If so, you should just be able to use:

    Code (csharp):
    1.  
    2.  
    3. GetComponent(fireScript).enabled = false;
    4.  
    5.  
    If that still gives you trouble (it might) you might want to separate your code up:

    Code (csharp):
    1.  
    2.  
    3. script2 : fireScript = GetComponent(fireScript);
    4.  
    5. fireScript.enabled = false;
    6.  
    7.  
    those are all javascript code, btw
     
  4. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    Another alternative (this is javascript too)

    Code (csharp):
    1.  
    2. (GetComponent(fireScript) as fireScript).enabled = false;
    3.  
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It all stems from the fact that you're passing GetComponent a string which is both slower and only returns a Component instead of whatever type you're trying to get at. If you pass a type or use the generic method the explicit cast using "as" should be unnecessary.
     
  6. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    @Kelso

    Read above, he also tried the Type overload and still didn't work.
     
  7. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    Yes, two scripts are attached to one object. The first code you mentioned is almost the same as the one I wrote in the main post, however I tried to avoid putting quotation marks around the script name as you said (so I wrote (fireScript) instead of ("fireScript") ), but it worsen the issue, I got the following error:
    BCE0018: The name 'fireScript' does not denote a valid type ('not found').


    Code (csharp):
    1.  
    2.  
    3. script2 : fireScript = GetComponent(fireScript);
    4.  
    5. fireScript.enabled = false;
    6.  
    7.  
    I also tried your second solution, Even though I didn't understand (script2 : fireScript) this type of variable declaration (Base on my understanding the word that comes after the 'colon colon' is the Variable-Type and fireScript can not be a variable type) but I tried this code anyway. but this also gave me the same error:
    BCE0018: The name 'fireScript' does not denote a valid type ('not found').
     
  8. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    AHHH guys I think I found the solution. My build setting was on Flash as I was creating my game to enter the Flash in a Flash contest. Now that I switched the platform to PC and Mac standalone, it seems that the first code I wrote in the post works perfectly.
    However I don't know whether I am going to face the problem with iOS platform or not.
    But thanks all of you for your replies
     
  9. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    You won't.
     
  10. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    Hi again guys,
    The problem is now happening for the iOS platform. I switch my built setting to iOS and I get ther same error I mentioned in first post. I tried all the solutions that is presented on this thread but none of them did work. Basically anywhere that I used GetCompenent("fireScript").enabled the error "'enabled' is not a member of 'UnityEngine.Component'" was generated. This also happened where I tried to set an AudioSource component which was attached to this object, to false.
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Don't use the string overload of GetComponent or just cast the result to fireScript (first option is better).
     
  12. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    @KelsoMRK
    I didn't get what you mean by this!!!

    I didn't get this part either but if you mean to combine the two scripts together, I have to say I cannot because the second script is a C# script while my entire game is written in JS.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't use strings in GetComponent.

    --Eric
     
  14. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    well in that case I have to use either this:

    Code (csharp):
    1. (GetComponent(fireScript) as fireScript).enabled = false;
    OR
    Code (csharp):
    1. ((fireScript)gameObject.GetComponent<fireScript>()).enabled = false;
    None of them work. All of them gives me an error!
     
  15. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    The second sample is C# code, not Javascript, hence the error. What error does the first sample produce?
     
  16. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Code (csharp):
    1. GetComponent.<fireScript>().enabled = false;
    Note the extra period when using javascript.

    Code (csharp):
    1. gameObject.GetComponent(fireScript).enabled = false;
    Should work great(assuming that the script is actually name fireScript and not FireScript.)
     
  17. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If you pass it a Type you don't have to cast.

    Code (csharp):
    1.  
    2. GetComponent(fireScript).enabled = false;
    3.  
    This should work fine - assuming a script named fireScript is attached to the GameObject.

    If you give it a string it casts to Component which doesn't have an enabled property - hence the error.


    EDIT: Get out of my head Hairball!
     
  18. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    Code (csharp):
    1. GetComponent(fireScript).enabled = false;
    it gives "Unknown identifier 'fireScript' " error.

    I have to remind you that this errorS only happens when I switch the platform to iOS. Otherwise on the standalone and web platforms the code
    Code (csharp):
    1. GetComponent("fireScript").enabled = false;    // with quotation mark!
    ...works fine.
    thus if you have iOS add-on try to test the same thing and see if it works.
    then let me know ;)

    Regards
     
    Last edited: Mar 1, 2012
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  20. shadowpuppet

    shadowpuppet

    Joined:
    May 7, 2015
    Posts:
    23
    I'm reading this and it really feels like it should solve my problem but it isn't. i basically want to do the same thing. Kill a script running on an object with another script. Situation is I have a legacy animated enemy doing an Enemy Behaviour script so that he spots me, runs at me and starts to beat on me. that all works fine but I am unable to make him stop. i fixed a collider to the players hand that when he punch the enemy , he starts to react ( die) but I can see that the attacking script on the enemy is still running. So...with other game objects I am able to make objects enable = false in the animation window but the enemy is read only. SO I need to put a script on the enemy itself to kill the attacking script. i have spent 3 days and everything I have tried fails - including everything here. Basically the script reads OnTriggerEnter a collider tagged fist" needs to disable the script called EnemyBehaviour and run the animation "enemDie". When Iturn off the EnemyBehaviour script the enemy dies when I unch him so I know it's all good up to th point of turning off the EnemyBehaviour scriptEnemyBehaviour is a C# and my script is java. Maybe that's the problem?
     
  21. AcesMan007

    AcesMan007

    Joined:
    Jul 15, 2014
    Posts:
    3
    I had the same problem, I ended up using get component but in type have the assets directory of the script.

    player.GetComponent.<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter>().enabled = false;

    ThirdPersonCharacter is the script I am disabling. I hope this helps and is what you are looking for.
     
  22. vfxjex

    vfxjex

    Joined:
    Jun 3, 2013
    Posts:
    93
    Disabling a script only works for Updates and probably Start.
    If you created your own method, that method can still be called even the script is disabled.
    probably what you need is to add boolean to activate and deactivate a method inside a script.
    if any mono-behavior method is not a part of your script, enabled could probably not a member of it, and
    you have to use boolean instead.

    you can check your script in the inspector if it has a check box. if no check box then you probably not using any mono-behavior method like Update and Start.
     
  23. hdr25

    hdr25

    Joined:
    Jan 12, 2017
    Posts:
    1
    Things are different now in Unity 5
    Its simple .. First you have to get that component and then get access to its script..

    GameObject.FindGameObjectWithTag ("YOUR GAME OBJECT").GetComponent<YOUR SCRIT NAME> ().enabled = false;
     
    ai190244 likes this.
  24. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Things aren't any different in Unity 5, regarding this subject. All of the previous posts still apply. Also the code you wrote was C# and doesn't apply to this topic, which is about Unityscript (Javascript).

    --Eric
     
  25. Aaaabbbcc

    Aaaabbbcc

    Joined:
    Jul 27, 2017
    Posts:
    1
    I pissed a whole day away with this problem setting up a S*** ton of various test cases and only when I was ready to give up (which I NEVER do) did I suddenly entertain the idea that something that I could not see in the script might somehow disable communication with the inspector yet still operate. So I simply cut and pasted the the script into a new script (different name of course), got rid of the old script, changed all references to the old script to the new name and now it works perfectly. Given my experience, I would start with this one before getting fancy.