Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jump/Move Sprite Update Error Unity 5 2D

Discussion in 'Scripting' started by Gabe-Tucker, Dec 22, 2015.

  1. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    I'm making a 2D game. So far, all there is is a 2D character with six sprites (and the ground). However, two of the sprites aren't loading in; the 'playerJumpingRightSprite' and the 'playerJumpingLeftSprite'. These should be activated when the character is moving and jumping at the same time. Here's the important/messed up part of the script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerMove : MonoBehaviour
    5. {
    6.     public Sprite playerJumpingLeftSprite;
    7.     public Sprite playerJumpingRightSprite;
    8.  
    9.     void Update ()
    10.     {
    11.         if((Input.GetKeyDown(KeyCode.Space) || (Input.GetKeyDown(KeyCode.W)) || (Input.GetKeyDown(KeyCode.UpArrow))) && isJumping == false)
    12.         {
    13.             GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight);
    14.             isJumping = true;
    15.             //playerJumpingLeftSprite
    16.             if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
    17.             {
    18.                 gameObject.GetComponent<SpriteRenderer>().sprite = playerJumpingLeftSprite;
    19.             }
    20.             //playerJumpingRightSprite
    21.             if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
    22.             {
    23.                 gameObject.GetComponent<SpriteRenderer>().sprite = playerJumpingRightSprite;
    24.             }
    25.         }
    26.     }
    27. }
    28.  
    Any help on why the two sprites aren't loading will be great. Thanks.
     
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Hey. Firstly, please never use GetComponent on update or more then once per component in the same class. This and Instantiating/Destroying objects are a huge performance killer as they add up very fast. Just create 2 private variables and use GetComponent in awake.

    Secondly I think you might have jumped to the wrong conclusion. I don't think they don't load. What might be happening is that you do not have the right sorting layer and your object is obstructed by another. Try changing this:
    http://prntscr.com/9h45gv
     
  3. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    Thank you, CloudKid. I poked around a bit longer, and realized that the playerJumpRight/LeftSprites were conflicting with my playerIdleRight/LeftSprites, so I stated to the Idle sprites that they would only play if the player was not jumping. This fixed up the main problem. I will use your suggestions in the first paragraph to improve my code. Thanks again!