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

NullReferenceException: Object reference not set to an instance of an object...

Discussion in 'Scripting' started by SewGracefully1998, Jan 23, 2018.

  1. SewGracefully1998

    SewGracefully1998

    Joined:
    Dec 1, 2017
    Posts:
    5
    Hi and thank you in advance for any help on this. New to C#. Trying to understand this error from the code below. I was doing a tutorial and attempted to alter the code, thinking I understood what I was doing. :[ Any help appreciated.

    Trying to create dragging of a player object across the screen and limit how far the object could pull back. I hope it's ok to post this question... I looked all over for posting etiquette beforehand. Apologies if I offend anyone.

    Receiving the following error:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour {
    7.  
    8.     public Rigidbody2D rb;
    9.     public float maxStretch = 3.0f;
    10.     public float releaseTime = .15f;
    11.  
    12.     private bool isPressed = false;
    13.     private Ray rayToMouse;
    14.     private Transform sling;
    15.     private float maxStretchSqr;
    16.  
    17.     void Start()
    18.     {
    19.         [COLOR=#ff0000]rayToMouse = new Ray(sling.position, Vector3.zero);[/COLOR]
    20.         maxStretchSqr = maxStretch * maxStretch;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         if (isPressed)
    26.         {
    27.             rb.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    28.         }
    29.     }
    30.  
    31.     void OnMouseDown()
    32.     {
    33.         isPressed = true;
    34.         rb.isKinematic = true;
    35.     }
    36.  
    37.     void OnMouseUp()
    38.     {
    39.         isPressed = false;
    40.         rb.isKinematic = false;
    41.         StartCoroutine(Release());
    42.     }
    43.  
    44.     IEnumerator Release ()
    45.     {
    46.         yield return new WaitForSeconds(releaseTime);
    47.         GetComponent<SpringJoint2D>().enabled = false;
    48.     }
    49.  
    50.     void Dragging()
    51.     {
    52.         Vector3 mouseWP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    53.         Vector2 slingToMouse = mouseWP - sling.position;
    54.  
    55.         if (slingToMouse.sqrMagnitude > maxStretchSqr)
    56.         {
    57.             rayToMouse.direction = slingToMouse;
    58.             mouseWP = rayToMouse.GetPoint(maxStretch);
    59.         }
    60.  
    61.         mouseWP.z = 0f;
    62.         transform.position = mouseWP;
    63.     }
    64. }
     
  2. SewGracefully1998

    SewGracefully1998

    Joined:
    Dec 1, 2017
    Posts:
    5
    The reference to the error says line 18, however, it's highlighted in green in the code and is showing as line 19. TY
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Chances are sling is null. How are you assigning a value to sling?
     
  4. SewGracefully1998

    SewGracefully1998

    Joined:
    Dec 1, 2017
    Posts:
    5
    I added the following to my code and error ceased. Thank you for your reply!

    Code (CSharp):
    1.  private SpringJoint2D spring;
    2.  
    3.     void Awake()
    4.     {
    5.         spring = GetComponent<SpringJoint2D>();
    6.         sling = spring.connectedBody.transform;
    7.     }