Variable Frustrations

Updated on January 9, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
11 on January 9, 2017

In my script I am declaring a variable and then changing it in the lines below. In most of the other languages I’ve coded in this would be perfectly fine but for some reason it keeps defaulting back to 1. I can not for the life of me figure out why this would be the case. Is it because I’m using Start when I should be using something else? or is this just the way unity works?

private int direction = 1;

void Start() {
if (Input.GetKey (KeyCode.A)){
direction = -1;
}
if (Input.GetKey (KeyCode.D)){
direction = 1;
}
}

  • Liked by
Reply
5 on January 9, 2017

The start method only gets called once at the start of the scene hence it’s name. Change Start to Update and I think that will fix it

Helpful
on January 9, 2017

Just reinforcing Job’s reply to this, it does appear this way, Update is called once every frame, and start is called upon initialisation of the script, what is happening here is that you are saying that direction is equal to -1 on the script start-up, and it is then constantly setting direction to 1 upon statement trigger.

Master
on January 9, 2017

Well that’s not exactly what is happening. The biggest chance is that it never even changes to -1 since it’s pretty hard to hit a button at the right moment to trigger the statement when it relies on user input in the Start method.

Helpful
on January 9, 2017

I don’t know what happening to me, i thought i read an update method and everything, proof that i become visually impaired after one sleepless night xD

on January 9, 2017

Actually. this isn’t the way this is working for me at all, the script above is to choose a direction for when my character shoots (I’m following the 2D sidescroller tutorial but I want a shooting system more like megaman, where you can only shoot left or right.) With the above code I can do that as long as I am holding the key down, but as soon as I let go it will default back to the original value that the variable was declared with.

Master
on January 9, 2017

I tested your code and for me I get the expected behavior nothing happens with the if statement in start since it’s close to impossible to trigger the statement with user input. But I do get the expected result when this is in the Update method. pressing a sets direction to -1 and when I release the key it stays on -1 same with the d key.

Show more replies
  • Liked by
Reply
Cancel
4 on January 9, 2017

Perhaps your variables need to start with a capital letter?

Devoted
on January 9, 2017

I understand you prefer capital letters, but don’t make other people use it. It’s best to use conventions. If you used to use capital letters it’s ok, but don’t make other people use it.

Helpful
on January 9, 2017

It’s always best to use capital letters

Master
on January 9, 2017

That’s not true at all. As said by Aviram the convention is not to start with a capital. But if someone prefers to do so he/she is allowed to do so.
 
You can’t say which one is better since there is none better then the other. It’s mostly personal preference.

Devoted
on January 9, 2017

Geezmo, it’s not best to use a capital letter. It’s best to get used to the convention, since it’s much easier for most people to read, and it will be much easier to find/work with teammates.

If you are used to it, fine, do whatever you want, but it’s absolutely terrible to try to get a guy to do something that’s not recommended. The default should be to follow the conventions, but exceptions can be made. For him, since he is new, his default would become something that’s not recommended, which is not good.

Show more replies
  • Liked by
Reply
Cancel