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

Bug Script ignores line of code unless in editor?

Discussion in 'Scripting' started by BlockManBlue, Sep 28, 2021.

  1. BlockManBlue

    BlockManBlue

    Joined:
    Feb 10, 2021
    Posts:
    3
    So I don't know why, but when I build my game, it completely ignores a specific line of code, no matter where i put it.
    (yes everything is defined, and no I'm not seeing any errors)
    Code (CSharp):
    1. public void respawn()
    2.     {
    3.         Health = 100;
    4.         dead = false;
    5.         deathScreen.SetActive(false);
    6.         Cursor.lockState = CursorLockMode.Locked;
    7.         Cursor.visible = false;
    8.         Time.timeScale = 1f;
    9.         GunScript.justUnpaused = true;
    10.         PaintGun.justUnpaused = true;
    11.         SpawnGunScript.justUnpaused = true;
    12.         DeleteGunScript.justUnpaused = true;
    13.         GravityGunScript.justUnpaused = true;
    14.         HealScript.justUnpaused = true;
    15.         GrenadeThrower.justUnpaused = true;
    16.         player.position = spawnPoint;
    17.     }
    It just completely ignores player.position = spawnPoint, anyone know how to fix this?
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    200
    If this works in editor and not in build then spawnPoint might not have serialized properly.
    Add a debug statement and check output.txt in build folder.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It is a common mistake among newbie programmers to see some sort of bug, reason that "if X happened, that would explain this bug", and then assume that X is true without looking for other things that could also explain the same symptom.

    The computer randomly deciding to skip a line of code is not a plausible hypothesis. I don't know what symptom you actually saw, but unless you stepped through this code in a debugger one line at a time and watched it jump from line 15 to line 17 without stopping on line 16, I think odds are high that you have misinterpreted the cause.

    Other possibilities include:
    • The variable "player" doesn't have the value you expected
    • The variable "spawnPoint" doesn't have the value you expected
    • This whole function isn't running at the time that you expected it to run
    • Something else changed the player's position after this code ran
    • A runtime exception was thrown on an earlier line, causing the function to end prematurely (unlikely; nothing in the code you posted is likely to throw an exception)