- Home /
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 :
- see if the player is in the cube(with isTrigger on)
- if he pres e go and fish
- if he is fishing generate random waits (for every tug)
- 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;
}
}
"it goes the wrong way"? What goes the wrong way, and what's the right way?
Answer by skovacs1 · Nov 08, 2010 at 07:23 PM
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...
- 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)
andfunction OnTriggerExit(other : Collider)
. - Your OnTrigger...s do not check the object. This means that when anything enters or exits, it will change your boolean.
- 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;
}
thanks i try to understand what i did wrong i think i messt up the trigger Enter and Exit and i run the function fish to much right?
i am only 14 but i try to understand everything :)
and thanks for helping me i was about 3 houres bussy to fix my problem :)
i have 1 question how can i make a bar like if he waits for the first time the point int he bar is in the green etc and he is rising towards the red(then you can catch the fish)?
Progress bars can be done any number of ways, just search 'progress bar' (http://answers.unity3d.com/search?q=progress+bar) and you'll find a few. Personally, I like the shader approach given here (http://www.unifycommunity.com/wiki/index.php?title=Reveal_Non-Uniform_Texture).
Your answer
![](https://koobas.hobune.stream/wayback/20220613082236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sniper zoom when right click? 1 Answer
Unity freezes when I play or run 1 Answer
Resouce script help 3 Answers
My project isn't saving. 1 Answer
How do I create a new object in the Editor as a child of another object? 1 Answer