I Found what the problem was... I was actually turning the shootTrigger to false inside another class. Problem solved.
Script execution order of child and parent classes - Parent then children???
HI there. I have a parent class called Weapon(), which is non-instantiable. NormalWeapon() derives from it. Only NormalWeapon has charge. The code is this:
Weapon Update():
if (shootTrigger == true)
shootTrigger = false;
else if (InputController.GetKeyDown (Buttons.shoot))
shootTrigger = true;
That is, I want shootTrigger to be true only for a frame and NormalWeapon to get this info, so in NormalWeapon Update() we have:
if (shootTrigger)
charging = true;
"charging" never happened to be true. So in Script Execution Order I put:
Weapon (100)
NormalWeapon (200)
Still, charging never happened to be true. NormalWeapon's charging gets the true value only if I put the shootTrigger verification in NormalWeapon's LateUpdate().
The impression I'm getting is this: parent Update() executes then children Update() executes Even if this order is changed in the Project Settings???
Thank you!
@haskell33 I believe script execution order is only helpful with start and awake methods, etc. not updates. That is so you can run scripts that do initialization, say something that loads data, before any other script that depends on the data being loaded runs.
Answer by b1gry4n · Apr 05, 2016 at 08:42 PM
You could just call a virtual function instead of trying to balance between false and true and getting mixed up.
Your "NormalWeapon" script derives from "Weapon" correct? Meaning the top of your NormalWeapon script it looks like this?
public class NormalWeapon : Weapon {
Why not just do this in weapon:
public class Weapon : MonoBehaviour {
bool shootTrigger = false;
void Update()
{
if (InputController.GetKeyDown(Buttons.shoot) && !shootTrigger) {
shootTrigger = true;
Shoot();
}
}
public virtual void Shoot()
{
}
}
and in your normal weapon script...
public class NormalWeapon: Weapon {
bool charge = false;
public override void Shoot()
{
charge = true;
shootTrigger = false;
}
}
Another thing too...GetButtonUp might work better for you. If you want the user to have to click each time shootTrigger = true, GetButtonUp waits until the button has been lifted. A button has 3 states... GetButton (called every frame the button is being held), GetButtonDown (Called the frame when the button is pressed) and GetButtonUp (called when the button is lifted). These have an order of their own by the nature of how a user is clicking the physical button, be it a key on a keyboard, mouse, touchscreen, nuclear missile launch button.
GetButtonDown -> GetButton -> GetButtonUp
Your code is way more elegant than $$anonymous$$e, I'll end up s$$anonymous$$ling that for myself lol Thanks!
About the GetButtonUp, yeah I'm using that already so that "charging" can get set to false, I'll probably get rid of this shootTrigger variable (didn't know why I came up with this in first place lol).
About the script execution order, that's still a mystery to me. Regarding this particular situation, that is. I get that your code solution will always work, but I'm still curious about why setting the Script Execution Order in Project Settings didn't work for me. And I should correct myself about the last statement: "The impression I'm getting is this: parent Update() executes then children Update() executes Even if this order is changed in the Project Settings???" If this was actually true, the execution order would be Weapon => Normal Weapon => Weapon => Normal Weapon etc. just like I wanted it to be. What I'm actually getting is Weapon => Weapon => Normal Weapon => Normal Weapon etc. Weird...