Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by PaxStyle · Mar 10, 2014 at 03:18 PM · disable

Disable script.

I open new question because i've not received any reply.

I drag this script to one gameobject with its script, the script works, because the variable scriptToControl turns on and off the required script, but the script don't disable.. it's works despite is Off. Anyone can help me? Thank you in advance

 #pragma strict
 
 var scriptToControl : Serranda;
 
 function Start () {
      scriptToControl = GetComponent("Serranda");
 }
 
 function Update() {
     if (Input.GetKeyDown(KeyCode.X)) {
           Debug.Log("User pressed X");
           if (scriptToControl.enabled)
                    scriptToControl.enabled = false;
           else
                    scriptToControl.enabled = true;
      }
 }
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 10, 2014 at 03:19 PM 0
Share

When you say "it works" what do you mean? Disabling just stops Coroutines, Update and LateUpdate. Anything else would need to test for the enabled state itself.

avatar image Calum-McManus · Mar 10, 2014 at 03:25 PM 0
Share

So this is a script that disables its self then try's to turn it self back on, if so, once the script is disabled it will no longer run when you press X so you can't turn it back on, because the script is turned off

avatar image whydoidoit · Mar 10, 2014 at 03:31 PM 1
Share

It appears to be disabling a second script, not itself. The OP also says that the script isn't disabled - which would appear to be a reference to things like collision functions still running on it, these are not affected by enabled.

avatar image PaxStyle · Mar 10, 2014 at 04:20 PM 0
Share

i just say that the script "serranda" is disabled if i press X, but it continue to run also if is disabled. :)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by edve98 · Mar 12, 2014 at 05:49 PM

This could be because unity doesn't really disable all functions, for example function Update (Well, in your case it's OnMouseDown). You can use function Start with infinite while loop or something, so it could be disabled.

If you are not using several serranda scripts, you can do like this:

in serranda script put static variable IsOn and if statement in function OnMouseDown. This should look like this:

 static var IsOn : boolean = false; //change to true, if you want thias script active from start
 
 //your other varaibles/script here till function OnMouseDown
 
 function OnMouseDown(){
     
     if(IsOn == true){
         
         //your script here from function OnMouseDown
         
     }
     
     
 }
 
 //more of your script here

and in your toggle script make just this:

 function Update(){
     
     if(Input.GetKeyDown(KeyCode.X)){
         
         serranda.IsOn = !serranda.IsOn;
         
     }
 }


And that is it! Of course you could add more neat stuff like not using static variables and making variable witch let's choose what script you want to control, so you can use more than one of the same script in one scene, but I'll leave it for you. Good luck!

Comment
Add comment · Show 22 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PaxStyle · Mar 12, 2014 at 06:26 PM 0
Share

?? so i have to remove the funtion update?

avatar image PaxStyle · Mar 12, 2014 at 06:38 PM 0
Share

i tried also this way, but nothing...

 #pragma strict
  
 var scriptToControl : Serranda;
 var on : boolean; 
  
 function Start () {
      scriptToControl = GetComponent("Serranda");
      scriptToControl.enabled = false;
 }
  
 function Update() {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
     
     on = !on;
     
     if (!on){
     scriptToControl.enabled = false;
     }
     
     if (on){
     scriptToControl.enabled = true;
     }
 }
 }
avatar image PaxStyle · Mar 13, 2014 at 02:51 PM 0
Share

@edve98 i don't see your reply

avatar image edve98 · Mar 13, 2014 at 02:55 PM 0
Share

Could you please post your serranda script? I think the problem is there.

avatar image edve98 · Mar 14, 2014 at 05:51 PM 1
Share

O$$anonymous$$, here you go:

 #pragma strict
 
 private var decrement : float;
 private var toggle : boolean;
 private var ray : Ray;
 private var hitInfo : RaycastHit;
 var Audio : AudioClip;
 
 function Start()
 {
     decrement = transform.localScale.y / 1.3f; //modificare questo valore per ridurre l'altezza.
 } 
 
 function On$$anonymous$$ouseDown()
 {   
     ray = Camera.main.ScreenPointToRay(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
 
     if(Physics.Raycast(ray, hitInfo))
     {
        if(hitInfo.transform.gameObject == gameObject)
        {
          if(Input.Get$$anonymous$$ouseButton)
            {
           if(!toggle)
           {
               transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y - decrement, transform.localScale.z);
               transform.position.y += decrement / 2;
               toggle = true;
               audio.clip = Audio;
                     audio.Play();
           }
 
           else
           {
               transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y + decrement, transform.localScale.z);
               transform.position.y -= decrement / 2;
               toggle = false;
               audio.clip = Audio;
                     audio.Play();
           }
          }
        }
     }
 }

I think that he should add if statement with static boolean variable IsON, because I think unity doesn't disable function On$$anonymous$$ouseDown. I'll post how this should look soon.

Show more comments
avatar image
0

Answer by whydoidoit · Mar 10, 2014 at 04:49 PM

If you are assigning the script using the inspector then you should remove your Start function which replaces the inspector set value with the named component attached to this scripts Game Object or null if doesn't have one.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PaxStyle · Mar 10, 2014 at 06:04 PM 0
Share

I don't understand, i drag the script that i posted in the gameobject where there is the script to disable(Serranda). What do i do now?

alt text

immagine.png (101.8 kB)
avatar image
0

Answer by NeatWolf · Mar 14, 2014 at 07:27 PM

I'm more experienced in c#, anyway: In your Serranda class declare another variable

 public var serrandaEnabled = true; //or False, depending on the initial state you desire

and in every function inside Serranda, in this case function OnMouseDown(), check its value:

 function OnMouseDown()
 {
 if (!serrandaEnabled)
 return;
 
 [rest of the existing code here]
 }

now, you simply have to change the lines with

 scriptToControl.enabled = false; //or true

to

 scriptToControl.serrandaEnabled = false; //or true

this should solve the problem.

EDIT: Or, as suggested, simply put at the beginning inside your OnMouseDown function:

 if (!enabled)
 return;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 14, 2014 at 07:29 PM 1
Share

It would be easier just to put:

    if(!enabled) return;

And use the normal enabled function that will automatically disable many parts of the script.

avatar image NeatWolf · Mar 14, 2014 at 07:57 PM 0
Share

Yes, indeed, that would be easier, you're totally right.

$$anonymous$$y habits somehow biased my answer :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

whats wrong with this script. it wont work 1 Answer

Flight script problem 0 Answers

Forward movement rotated on movement script? 0 Answers

MainCharacter Enemy Following Script Problem 1 Answer

Enemy Health Damage 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges