- Home /
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.
I don't see any code here that disables a script. You must be talking about another script that tries to disable these?
Where is your OnTriggerEnter function that disables the scripts?
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
This isn't helpful. We need to see the code that disables it.
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;
thanks a lot. I will test it out later and send you the code that I used as well :D
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
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" );
}
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");
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