script act weird

Hallo my script changes a variable back to true but it says it goes to false.

i need help it is a fishing script but i can't found the problem.

here is simple explanation what i must do :

  1. see if the player is in the cube(with isTrigger on)
  2. if he pres e go and fish
  3. if he is fishing generate random waits (for every tug)
  4. after al those tugs reset the variables

after 3 it goes the wrong way please help me

here is the code

var target : Transform;
var FishingPool : Transform;
var rotationY :float= 0.0;

private var showGUI = false;
private var isFishing = false;
private var tugFish = false;
private var changeble = true;

function Update () {
    print("show gui:"+showGUI +"   isFishing:"+ isFishing +"   tugFish:"
          + tugFish +"   changeble :"+ changeble);
    if(showGUI) {
        if(Input.GetKeyDown("e")) {
            if(!isFishing) {
                target.GetComponent(ThirdPersonController).enabled = false;
                target.localEulerAngles.y = rotationY;
                target.animation.Stop();
                target.animation.Play("fishing");
                isFishing = true;
            } else {
                target.GetComponent(ThirdPersonController).enabled = true;
                isFishing = false;
            }
        }
        if(!isFishing)
            target.GetComponent(ThirdPersonController).enabled = true;
        else fish();
    }
    if(changeble) tugFish = false; 
}

function OnTriggerEnter() {
    showGUI = true;
}

function OnTriggerExit() {
    showGUI = false;
}

function OnGUI() {
    if(showGUI) {
        if(!isFishing)
            GUI.Label(Rect(Screen.width/2-50, 50,100,30),"Press E to Fish");
        else
            GUI.Label(Rect(Screen.width/2-75, 50,150,30),
                      "Press E to cancel fishing");

        if(isFishing) {
            if(tugFish && GUI.Button(Rect(Screen.width/2 +50,50,60,30),
                                     "PULL!")) {
            }
            else if(GUI.Button(Rect(Screen.width/2 +50,50,60,30),"PULL!")) {
                tugFish = false;
                isFishing = false;
                changeble = true;
            }
        }
    }
}

function restart() {
    yield WaitForSeconds(2);
    target.GetComponent(ThirdPersonController).enabled = true;
}

function fish() {
    if(isFishing) {
        changeble = false;
        var tug1 =  Random.Range(7, 12);
        var tug2 =  Random.Range(6, 10);
        var tug3 =  Random.Range(1, 2);

        yield WaitForSeconds(tug1);
        target.animation.Play("tug");

        yield WaitForSeconds(tug2);
        target.animation.Play("tug");
        tugFish = true;

        yield WaitForSeconds(tug3);
        changeble = true;

        yield;
        isFishing = false;
        tugFish = false;
    }
}

There are several things to consider that are troublesome with your script. In my answers, I'll spell English words correctly, even if your variables are not named in proper English words, so please correct for your usage wherever appropriate:

OnTrigger...

  1. OnTriggerEnter and OnTriggerExit take a Collider. In order for your functions to override the built-in functions, you need to define functions with the same signatures and therefore taking in Colliders `function OnTriggerEnter(other : Collider)` and `function OnTriggerExit(other : Collider)`.
  2. Your OnTrigger...s do not check the object. This means that when anything enters or exits, it will change your boolean.
  3. As per the documentation on OnTrigger... functions, one of the two objects will need a RigidBody or CharacterController.

Tracing your code paths

State:

First run:

  • showGUI is false.
  • changeable is true.
  • Every frame, Update sets tugFish to false.

OnTriggerEnter:

  • showGUI is true.
  • isFishing is false.
  • changeable is true.
  • Every GUI update, OnGUI will show a label "Press E to Fish".
  • Every frame, Update, target's thirdPersonController component is found and enabled and tugFish is set to false.

OnTriggerEnter, then OnTriggerExit:

  • showGUI is false.
  • isFishing is false.
  • changeable is true.
  • target's thirdPersonController component is still enabled.
  • Every frame, tugFish is set to false.

OnTriggerEnter, then key "e" is pressed down:

  • showGUI is true.
  • isFishing is false.
  • changeable is true.
  • The first frame that e is pressed down, Update finds target's thirdPersonController component and disables it, stuff happens and isFishing is set to true and then fish is called.
  • fish sets changeable to false and then yields.
  • Every GUI update after this OnGUI will show a label "Press E to cancel fishing" and a button which will set is fishing to false, tugFish to false(which it already is or you wouldn't have the button) and changeable to true.

OnTriggerEnter after "e" is pressed down:

  • showGUI is true.
  • isFishing is true.
  • changeable is false.
  • Every frame Update calls fish().
  • Every GUI update, OnGUI will show a label "Press E to cancel fishing" and a button which will set is fishing to false, tugFish to false(which it already is or you wouldn't have the button) and changeable to true.
  • fish will yield twice, after which tugFish will be true and fish will yield. OnGUI will now show a button that doesn't do anything.
  • fish will set changeable to true and yield again. Update will set tugFish to false.
  • fish will set isFishing to false and tugFish to false (which it already is).
  • Every GUI update, OnGUI will show a label "Press E to Fish".
  • Update the next frame will not call fish, but will find target's thirdPersonController component and disable it.

If anything should ever exit your trigger during any of these intermediate states, the script will essentially fall apart. fish will be left at the last yield that was encountered and if ever called after this will resume from there. fishing may still be true, changeable may still be false, tugFish may still be true and the thirdPersonController would still be disabled and the GUI would show nothing to let them know what has happened.

Your logic is a mess. You set variables to values when they aren't even changing and you depend on a certain setup but then don't properly reset it when you're done. You use yield statements in a function which really should be in a coroutine.

Here's something more sensible:

var target : Transform;
var rotationY : float= 0.0;

private var inside : boolean = false;
private var fishing : boolean = false;
private var fishOnHook : boolean = false;

function OnTriggerEnter(other : Collider) {
    //Check what it is
    inside = true;
}

function OnTriggerExit(other : Collider) {
    //Check what it is
    StopFishing();
    inside = false;
}

function OnGUI() {
    if(inside) {
        if(!fishing)
            GUI.Label(Rect(Screen.width/2-50, 50,100,30),"Press E to Fish");
        else {
            GUI.Label(Rect(Screen.width/2-75, 50,150,30),
                      "Press E to cancel fishing");
            if(GUI.Button(Rect(Screen.width/2 +50,50,60,30),"PULL!")) {
                if(fishOnHook) {
                    //do something
                }
                StopFishing();
            }
        }
    }
}

function Update () {
    if(inside && Input.GetKeyDown("e")) {
        if(!fishing) StartCoroutine("Fishing");
        else StopFishing();
    }
}

function StopFishing() {
    StopCoroutine("Fishing");
    target.GetComponent(ThirdPersonController).enabled = true;
    target.animation.Stop();
    fishOnHook = false;
    fishing = false;
}

function Fishing() {
    fishing = true;
    target.GetComponent(ThirdPersonController).enabled = false;
    target.localEulerAngles.y = rotationY;
    target.animation.Stop();
    target.animation.Play("fishing");

    yield WaitForSeconds(Random.Range(7, 12));
    target.animation.Play("tug");

    yield WaitForSeconds(Random.Range(6, 10));
    target.animation.Play("tug");
    fishOnHook = true;

    yield WaitForSeconds(Random.Range(1, 2));
    fishOnHook = false;
    target.GetComponent(ThirdPersonController).enabled = true;
    fishing = false;
}