Search Unity

Advance enemy

Discussion in 'Getting Started' started by simla1, Apr 19, 2017.

  1. simla1

    simla1

    Joined:
    Mar 12, 2017
    Posts:
    4
    I am playing Unity 2D, I wrote the following code to advance the enemy, but when I play the game the enemy does not advance or even disappears
     

    Attached Files:

  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome to the forums!

    First, if you're going to share code, it's best to use code tags. It makes it much easier for us to read it, copy and paste, and fix issues we might see. Use the button at the top of the text editor, shown here:


    It can often be helpful to share the entire script (if it's not ridiculously long), and any other scripts you have attached to that object. Also, make sure you post any errors that are showing up in the console (typically at the bottom of your Unity Editor window if you don't have the panel open already).

    One thing that jumps out at me right away is that you're multiplying by Time.deltaTime inside of FixedUpdate. You don't want to do that. Multiplying by Time.deltaTime is useful only inside the Update method, since that runs at a variable rate (the framerate your game is currently getting), where FixedUpdate runs at a locked rate in sync with the physics engine.

    Edit: As Joe pointed out, my previous paragraph isn't necessarily true. Don't believe everything you see on the Internet!

    In order to debug issues like this, you have a number of options:
    1. Insert logs to the console. Something like this, maybe:
      Code (CSharp):
      1. void Update() {
      2.   if (Input.GetKeyDown(KeyCode.L)) {
      3.     Debug.Log(gameObject.name + "'s position: " + transform.position);
      4.   }
      5. }
      Then whenever you press the L key, you'll get a message telling you where that object is.
    2. Attach MonoDevelop to the Unity process so you can set breakpoints. The process is a bit lengthy to type out here, but it's covered very well in this guide (and so is Debug.Log).
    3. Comment out sections of code piece by piece to see which one's causing the problem. If you comment out a line and the problem stops, you know that's the line doing something you don't want and can focus your efforts there.
    Don't be afraid to tinker with it and mess things up. That's how you learn. Plus, if the code isn't working, it isn't precious enough to preserve, anyway!

    Debugging and troubleshooting problems is a very important skill you'll want to have, and the sooner you get comfortable with it, the better your development experience will be!

    Good luck!
     
    Last edited: Apr 19, 2017
    simla1 likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hmm, I'm afraid I can't agree with this. Yes, FixedUpdate tries to run at a fixed rate (thus the name), but it can't always succeed; if there just aren't enough cycles to go around, it might run slower (though theoretically it should never run faster). And the fixed rate which it tries to maintain is not necessarily a constant; you can change it via the Time Manager (and maybe from code too — I'm not sure).

    So, it's a good idea to use Time.deltaTime in both Update and FixedUpdate, so that your code can maintain constant speeds and accelerations regardless of the actual frame (or fixed-update) rate.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Interesting. I wonder where I got my misinformation, then. I could have sworn I read that on something official before... o_O

    Goes to show how little I use physics in my game projects, I guess, too!
     
    JoeStrout likes this.
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The rest of @Schneider21's advice about debugging is spot-on.

    The only thing I might add is, make use of the Hierarchy and Inspector tabs while your game is running. Select the object that has this script on it, and see what's going on by inspecting it. You say it disappears — is it being destroyed? Moving off the screen? What exactly? The Inspector should tell you.
     
    Schneider21 likes this.
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I meant to say that and forgot! So obvious, but if you run your game in full screen view all the time, you may not even think to try it!