- Home /
Need help completely disabling a script
#pragma strict
var talkLines:String[];
var talkTextGUI:UnityEngine.UI.Text;
var textscrollSpeed:int;
private var talking:boolean;
private var textIsScrolling:boolean;
private var playerScript:UnityStandardAssets.Characters.FirstPerson.FirstPersonController;
private var currentLine:int;
function OnTriggerEnter(col:Collider){
if(col.tag == "Player"){
playerScript = col.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController);
Debug.Log(playerScript);
talking = true;
currentLine = 0;
//talkTextGUI.text = talkLines[currentLine]; //STATIC
startScrolling();
playerScript.enabled = false;
}
}
function Update () {
if(talking){
if(Input.GetButtonDown("Npcmt")){
if(textIsScrolling){
//display full line
talkTextGUI.text = talkLines[currentLine];
textIsScrolling = false;
}
else{
//display next line
if(currentLine < talkLines.Length - 1){
currentLine++;
//talkTextGUI.text = talkLines[currentLine]; //STATIC
startScrolling();
}
else{
currentLine = 0;
talkTextGUI.text = "";
talking = false;
playerScript.enabled = true;
}
}
}
}
}
function startScrolling(){
textIsScrolling = true;
var startLine:int = currentLine;
var displayText:String = "";
for(var i:int = 0; i < talkLines[currentLine].Length; i++){
if(textIsScrolling && currentLine == startLine){
displayText += talkLines[currentLine][i];
talkTextGUI.text = displayText;
yield WaitForSeconds(1/ textscrollSpeed);
}
}
}
Above is my NPC talking script in Unity.
#pragma strict
var script : NPCscript;
function Start() {
script = GetComponent(NPCscript);
script.enabled = false;
}
Above is my disabling script for the NPC script. The problem is that when I disable the script it doesn't fully work and it shows the first line of dialogue and you can't continue the dialogue. I want all the dialouge to be fully disabled and not to work. The variable talking and the variable textIsScrolling become true when the script is disabled when I talk to the NPC and I think that that is the problem.
Thanks, ~Jordan
P.S Both of these scripts are in JS.
Disabling a script simply stops the running of $$anonymous$$onoBehaviours (Update etc) but anything else will still run.
If you truly want to disable the script, place it on an object and disable the object.
The other common hacky way is to add something else like your talking
variable, say turnedOff
, and check for that at the start of OnTriggerEnter.
wasn't that in one of the release notes that collision and trigger callbacks are not called if the script is disabled?
$$anonymous$$aybe, don't know. But the current docs say OnCollision is intentionally called on a disabled script. Bottom of 2nd para: http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionEnter.html
Answer by Goatcheesecough · Oct 10, 2015 at 04:01 PM
Thanks everyone but I just ended up using meat5000's trick.
Answer by Statement · Oct 09, 2015 at 10:51 PM
Try adding this guard to OnTriggerEnter:
if (!enabled)
return;
IIRC, physics callbacks are still sent even though the script is disabled.
Your answer
Follow this Question
Related Questions
Flip over car after tips over? 1 Answer
I'm trying to display my ammo; not working 2 Answers
Realistic reloading system 1 Answer
Setting Scroll View Width GUILayout 1 Answer