Home > Enterprise >  How do I fix my c# script that has suddenly stopped working?
How do I fix my c# script that has suddenly stopped working?

Time:07-15

I'm trying to make a 2d platforming game using Unity game engine, and I've run into an issue with my code. One of the game's core mechanics is a shield the player has most of the time, and previously I was able to change the position of this shield using the arrow keys and it was working fine, but now it has suddenly stopped and I can't figure out why. Is there anything here I'm overlooking that's causing it?

The shield's script:

using UnityEngine;

public class Shield : MonoBehaviour
{
  //Sets the player's movement script as an object to be called on within this script.
  public Player_Movement player;

  // Update is called once per frame
  void Update()
  {
      if (Input.GetKeyDown(KeyCode.LeftArrow))
      {
          if (player.crouch)
          {
              this.transform.position = player.transform.position   new Vector3(-0.6f, -0.5f, 0);
              HoldShieldSide();
          }
          else
          {
              this.transform.position = player.transform.position   new Vector3(-0.6f, 0.5f, 0);
              HoldShieldSide();
          }
      }

      if (Input.GetKeyDown(KeyCode.UpArrow))
      {
          if (player.crouch)
          {
              this.transform.position = player.transform.position   new Vector3(0f, 0.1f, 0);
            HoldShieldUp();
          }
          else
          {
              this.transform.position = player.transform.position   new Vector3(0f, 1.1f, 0);
              HoldShieldUp();
          }
      }

      if (Input.GetKeyDown(KeyCode.RightArrow))
      {
          if (player.crouch)
          {
              this.transform.position = player.transform.position   new Vector3(0.6f, -0.5f, 0);
              HoldShieldSide();
          }
          else
          {
              this.transform.position = player.transform.position   new Vector3(0.6f, 0.5f, 0);
              HoldShieldSide();
          }
      }
  }

  //Lowers the shield to be in front of the bottom box collider.
  public void LowerShield()
  {
      print("LowerShield function is being read.");

      //If the player is facing the right...
      if (player.transform.localScale.x == 1)
      { 
          //...set the shield to the right side when crouched.
          this.transform.position = player.transform.position   new Vector3(0.6f, -0.5f, 0);
          HoldShieldSide();
      }
      //If the player is facing the left...
      else if (player.transform.localScale.x == -1)
      {
          //...set the shield to the left side when crouched.
          this.transform.position = player.transform.position   new Vector3(-0.6f, -0.5f, 0);
          HoldShieldSide();
      }
  }

 //Raises the shield to be in front of the top box collider.
  public void RaiseShield()
  {
      print("RaiseShield function is being read.");

      //If the player is facing the right...
      if (player.transform.localScale.x == 1)
      {
          //...set the shield to the right side when standing.
          this.transform.position = player.transform.position   new Vector3(0.6f, 0.5f, 0);
          HoldShieldSide();
      }
      //If the player is facing the left...
      else if (player.transform.localScale.x == -1)
      {
          //...set the shield to the left side when standing.
          this.transform.position = player.transform.position   new Vector3(-0.6f, 0.5f, 0);
          HoldShieldSide();
      }
  }

  //Keep the shield at its default rotation.
  public void HoldShieldSide()
  {
      this.transform.rotation = Quaternion.Euler(0, 0, 0);
  }

  //Rotate the shield by 90 degrees.
  public void HoldShieldUp()
  {
      this.transform.rotation = Quaternion.Euler(0, 0, 90f);
  }
}

CodePudding user response:

There are some tips I can give you to find the problem: Check that no other scripts are checking the position of the shield. You wrote the LowerShield () and RaiseShield () functions, but they both check if the player's localScale property is -1. This shouldn't be possible, because the scale of a GameObject must be positive In the code shown you never use them, so try deleting them for now. Insert in each block "if (player.crouch)", print to understand what happens. Then if you still don't understand, send us the console output.

using UnityEngine;

public class Shield : MonoBehaviour
{
    //Sets the player's movement script as an object to be called on within this script.
    public Player_Movement player;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (player.crouch)
            {
                print("LeftArrow  | "   "  Privious position:"   transform.position   "  |  New position: "   player.transform.position   new Vector3(-0.6f, -0.5f, 0)));
                this.transform.position = player.transform.position   new Vector3(-0.6f, -0.5f, 0);
                HoldShieldSide();
            }
            else
            {
                this.transform.position = player.transform.position   new Vector3(-0.6f, 0.5f, 0);
                HoldShieldSide();
            }
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (player.crouch)
            {
                print("UpArrow  | "   "  Privious position:"   transform.position   "  |  New position: "   (player.transform.position   new Vector3(0f, 0.1f, 0)));
                this.transform.position = player.transform.position   new Vector3(0f, 0.1f, 0);
                HoldShieldUp();
            }
            else
            {
                this.transform.position = player.transform.position   new Vector3(0f, 1.1f, 0);
                HoldShieldUp();
            }
        }

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (player.crouch)
            {
                print("RightArrow  | "   "  Privious position:"   transform.position   "  |  New position: "   (player.transform.position   new Vector3(0.6f, -0.5f, 0)));
                this.transform.position = player.transform.position   new Vector3(0.6f, -0.5f, 0);
                HoldShieldSide();
            }
            else
            {
                this.transform.position = player.transform.position   new Vector3(0.6f, 0.5f, 0);
                HoldShieldSide();
            }
        }
    }
    
    //Keep the shield at its default rotation.
    public void HoldShieldSide()
    {
        this.transform.rotation = Quaternion.Euler(0, 0, 0);
    }

    //Rotate the shield by 90 degrees.
    public void HoldShieldUp()
    {
        this.transform.rotation = Quaternion.Euler(0, 0, 90f);
    }
}

if you think my answer helped you, you can mark it as accepted and vote positively. I would very much appreciate it :)

  • Related