Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Odd issue with character controls when the character's gameobject is not selected in the editor.

Discussion in 'Scripting' started by DungeonBrickStudios, Oct 17, 2019.

  1. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    Hi guys,

    So I have a character controller I made from scratch, so it doesn't use rigid body physics or a character controller, relying more on raycasts and such.

    It works okay in principle, but when playtesting in the editor, if I don't have the character's gameobject selected, he won't jump. Left/right movement works fine all the time, and when the jump button is pushed, the jump animation plays, but the character does not fly up into the air for the jump.

    But, if I have the character's gameobject clicked/highlighted in the editor, he jumps just fine. The code for jumping is in the update method. So my question is, is there something related to focus or whatever that happens in the editor when you click on/off gameobjects that might cause this behavior?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You should probably post your jump code.

    One tricky detail I've noticed in the past is that selecting an object in the inspector will force any visible-in-editor fields on its components to be serialized and deserialized, which will implicitly convert null to default instances (and could conceivably cause other side-effects if you used any custom serialization). The lesson I took from that is "never try to set any visible-in-editor field to null".
     
    DungeonBrickStudios likes this.
  3. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    That's really good to know, but as it turns out it was unrelated.

    When you told me to post the code, some different methods handled different parts of the movement, so for instance in the update method I have jump input detection and the setting of the y velocity, but in the move method that is where those values are used. So as I was compiling the code to post, I found a snippet of code I thought was necessary which turned out to be the problem. No idea why it depended on whether or not the gameobject was clicked but there it was.

    So yeah really appreciate the help, just talking about an issue can lead to the solution without necessarily pointing a specific thing out sometimes.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
  5. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    Hah, yeah I've been using that forever without knowing that someone actually defined the concept. I explain problems to non-programmers around me in real life all the time and get the solution in the process.