Search Unity

Question how to refrence object

Discussion in 'Scripting' started by takocat13, Dec 17, 2022.

  1. takocat13

    takocat13

    Joined:
    May 22, 2022
    Posts:
    1
    hello im making a game where u are a cube animal thing and u have a gun and the gun is looking at mouse point but iv made my movement system so the character scale goes to the negative so the sprite flips but that inverst the gun controls and im trying to fix it. i got this error and i dont know how to fix it
    upload_2022-12-17_16-48-42.png
    heres all the scripts involved upload_2022-12-17_16-49-46.png
    upload_2022-12-17_16-50-6.png

    Code (CSharp):
    1. private float speed = 14f;
    2.     private float horizontal;
    3.     private float jumpingPower = 20f;
    4.  
    5.     private bool isWallSliding;
    6.     private float wallSlidingSpeed = 0.9f;
    7.  
    8.     private bool isWallJumping;
    9.     private float WallJumpingDirection;
    10.     private float WallJumpingTime = 0.2f;
    11.     private float WallJumpingCounter;
    12.     private float WallJumpingDuration = 0.4f;
    13.     private Vector2 WallJumpingPower = new Vector2(5f, 14);
    14.     private bool isFacingRight = true;
    15.  
    16.    
    17.     [SerializeField] private Rigidbody2D rb;
    18.     [SerializeField] private Transform groundCheck;
    19.     [SerializeField] private LayerMask groundLayer;
    20.     [SerializeField] private Transform wallCheck;
    21.     [SerializeField] private LayerMask wallLayer;
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  
    28.     void Update()
    29.     {
    30.         horizontal = Input.GetAxisRaw("Horizontal");
    31.  
    32.         if (Input.GetButtonDown("Jump") && IsGrounded())
    33.         {
    34.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    35.         }
    36.  
    37.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    38.         {
    39.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    40.         }
    41.  
    42.         WallSlide();
    43.         WallJump();
    44.         if(!isWallJumping)
    45.         {
    46.             Flip();
    47.         }
    48.        
    49.     }
    50.     private bool IsGrounded()
    51.     {
    52.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    53.  
    54.     }
    55.     private bool isWalled()
    56.     {
    57.         return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
    58.     }
    59.     private void WallSlide()
    60.     {
    61.         if(isWalled() && !IsGrounded() && horizontal != 0f)
    62.         {
    63.             isWallSliding = true;
    64.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
    65.         }
    66.         else
    67.         {
    68.             isWallSliding = false;
    69.         }
    70.     }
    71.     private void FixedUpdate()
    72.     {
    73.         if(!isWallJumping)
    74.         {
    75.             rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    76.         }
    77.        
    78.     }
    79.     private void WallJump()
    80.     {
    81.         if(isWallSliding)
    82.         {
    83.             isWallJumping = false;
    84.             WallJumpingDirection = -transform.localScale.x;
    85.             WallJumpingCounter = WallJumpingTime;
    86.             CancelInvoke(nameof(StopWallJumping));
    87.         }
    88.         else
    89.         {
    90.             WallJumpingCounter -= Time.deltaTime;
    91.         }
    92.         if(Input.GetButtonDown("Jump") && WallJumpingCounter > 0f)
    93.         {
    94.             isWallJumping = true;
    95.             rb.velocity = new Vector2(WallJumpingDirection * WallJumpingPower.x, WallJumpingPower.y);
    96.             WallJumpingCounter = 0f;
    97.             if(transform.localScale.x != WallJumpingDirection)
    98.             {
    99.                 isFacingRight = !isFacingRight;
    100.                 Vector3 localScale = transform.localScale;
    101.                 localScale.x *= -1f;
    102.                 transform.localScale = localScale;
    103.             }
    104.             Invoke(nameof(StopWallJumping), WallJumpingDuration);
    105.         }
    106.     }
    107.     private void StopWallJumping()
    108.     {
    109.         isWallJumping = false;
    110.     }
    111.     private void Flip()
    112.     {
    113.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    114.         {
    115.             isFacingRight = !isFacingRight;
    116.             Vector3 localScale = transform.localScale;
    117.             localScale.x *= -1f;
    118.             transform.localScale = localScale;
    119.         }
    120.        
    121.     }
    122. }
    sorry if most of its a dumb mistake im very new to working with unity and C#
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.