Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Gohst002 · May 20, 2017 at 08:48 AM · triggerdisabletimer-script

how do I disable a script

I wanted to stop two of my scripts when I enterd a trigger. One of the got disabled but the other one didn't. This is the one that did not get disable:

using System.Collections;

using UnityEngine;

using UnityEngine.UI;

using System.Collections.Generic;

public class Timer : MonoBehaviour {

public Text timerTxt;

public float startTime;

 // Use this for initialization
 void Start () {
     startTime = Time.time;
 }
 
 // Update is called once per frame
 void Update () {
     float t = Time.time - startTime;

     string minutes = ((int)t / 60).ToString    ();
     string seconds = (t % 60).ToString();
     timerTxt.text = minutes + seconds;    
 }

}

The one that did:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerMovement : MonoBehaviour { public Rigidbody rB;

 public float movementSpeed;

 public float turnSpeed;

 public float brakeSpeed;
 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void FixedUpdate () {
     Movement ();
 }
 public void Movement()
 {
     rB.AddRelativeForce (Vector3.right * movementSpeed);    

     if (Input.GetKey("s"))
     {
         rB.AddRelativeForce(Vector3.left * brakeSpeed);
     }

     if (Input.GetKey ("d")) {
         rB.AddRelativeForce (Vector3.back * turnSpeed);

     }
     if (Input.GetKey ("a")) {
         rB.AddRelativeForce (Vector3.forward * turnSpeed);
     }
     if (Input.GetKey("s"))
     {
         turnSpeed = 15f;
     }
 }

}

I also filled in all the objects etc.

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 Invertex · May 17, 2017 at 11:10 AM 0
Share

I don't see any code here that disables a script. You must be talking about another script that tries to disable these?

avatar image CrimOudin · May 17, 2017 at 11:23 AM 0
Share

Where is your OnTriggerEnter function that disables the scripts?

avatar image Gohst002 · May 17, 2017 at 12:20 PM 0
Share

ja these are the ones that should get disabled they are not the scripts that disable. The first one did not get disabled but the second one did

avatar image CrimOudin Gohst002 · May 17, 2017 at 12:29 PM 0
Share

This isn't helpful. We need to see the code that disables it.

3 Replies

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

Answer by BoogieD · May 17, 2017 at 01:36 PM

I can't relate your code to the question but you can disable a script Component or a GameObject by setting enabled to false. 'gameObject' is the GameObject that the script is attached to.

 var myScript = gameObject.GetComponent<MyScript> ();
 myScript.enabled = false;





Comment
Add comment · Show 3 · 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 Gohst002 · May 18, 2017 at 11:49 AM 0
Share

thanks a lot. I will test it out later and send you the code that I used as well :D

avatar image Invertex · May 18, 2017 at 11:52 AM 0
Share

Don't forget to check if(myScript != null) before doing that disable. Otherwise if that gameObject does not have that type of script, you will throw a null reference exception and wonder why your game is so buggy. There's no point in putting it into a variable if you're not going to actually use the variable for anything. It could have just been gameObject.GetComponent<$$anonymous$$yScript>().enabled = false; if you're not doing a null check

avatar image BoogieD Invertex · May 21, 2017 at 02:37 AM 0
Share

Yes, you make a valid point. A specific question has a specific answer but it also creates several issues to writing robust code. I left that out for clarification. It's best to always check for null prior to accessing objects unless you know it has been created or assigned correctly. $$anonymous$$ore useful code will give a debug message when the object is null.

 var myScript = gameObject.GetComponent<$$anonymous$$yScript> ();
 if( myScript != null ){
     myScript.enabled = false;
 }
 else{
     Debug.Log( "myScript == null" );
 }



avatar image
0

Answer by blake_seow · May 17, 2017 at 01:38 PM

what funtion you used for disable those scripts? I think best 2 ways to do this: (1) Have a static bool to control it. put this bool in xxx script, for example: public static bool disableScripts=false;

Then add a line in update() of those 2 scripts: if(xxx.disableScripts)this.enable=false;

(2) Use SendMessage to do it. So you will add a void in those 2 scripts: void CloseMe(){ this.enable=false; } Then send message to the gameobjects which has those scripts attached. xxxx.SendMessage("CloseMe");

Comment
Add comment · 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
0

Answer by Gohst002 · May 17, 2017 at 01:37 PM

ja these are the ones that should get disabled they are not the scripts that disable. The first one did not get disabled but the second one did

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

i need the animation to be trigger on a specific time but it is not trigger, script got no error. Not sure where is the problem, need help please. 1 Answer

Timer Continues After Object is disabled 2 Answers

How to Disable/Enable another Script with Triggers? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Turning script not enabling/disabling? 1 Answer


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