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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Declaring Vector 2 Problem

Discussion in '2D' started by Peti89, Dec 30, 2014.

  1. Peti89

    Peti89

    Joined:
    Jun 16, 2014
    Posts:
    3
    Hi

    This code gives me this error "NullReferenceException: Object reference not set to an instance of an object"
    Please Help me

    Code (JavaScript):
    1. public var spidy : GameObject;
    2. public var aim : GameObject;
    3. private var spidypos = Vector2(spidy.transform.position.x,spidy.transform.position.y);
    4. private var aimdir = Vector2(aim.transform.position.x,aim.transform.position.y);
    5. function Start () {
    6.  
    7. }
    8.  
    9. function Update ()
    10. {
    11.     if (Input.GetMouseButtonDown(0))
    12.     {
    13.         var hit: RaycastHit2D = Physics2D.Raycast(spidypos,aimdir,10);
    14.         if (hit.collider != null)
    15.         {
    16.             Debug.Log("Hit");
    17.         }
    18.         else
    19.         {
    20.             Debug.Log("Didn't hit");
    21.         }
    22.     }
    23. }
     
  2. Cyras

    Cyras

    Joined:
    Nov 26, 2012
    Posts:
    14
    Without knowing at which line the error occurs it's hard to tell where your problem is, but i would guess that you forgot to assign the gameobjects for "spidy" and "aim" in the editor.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't run code outside functions; spidypos should be only declared outside functions, and the value can be assigned in Start. However you don't actually need those variables, since Vector3 implicitly converts to Vector2. Also it's unlikely you'd want to do that anyway, since you'd be stuck with the values that were assigned in Start and I doubt that's what you want.

    Code (csharp):
    1. var hit: RaycastHit2D = Physics2D.Raycast(spidy.transform.position, aim.transform.position, 10);
    Note that this wouldn't actually work since Raycast uses a position and a direction. Either use a direction instead of aim.transform.position, or else use Linecast.

    --Eric
     
  4. ErikElgerot

    ErikElgerot

    Joined:
    Nov 24, 2014
    Posts:
    11
    Unless there's something funky with unityscript that I've missed (more of a c# dev myself) you seem to be missing the "new" keyword in there.

    So spidypos = new Vector2(...
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unityscript automatically implies "new" anyway, so writing it out is unnecessary.

    --Eric
     
    ErikElgerot likes this.