Search Unity

NullReferenceException in Ruby 2D Tutorial & Projectile won't move

Discussion in '2D' started by Gravano, Aug 11, 2019.

  1. Gravano

    Gravano

    Joined:
    Jan 2, 2018
    Posts:
    16
    I've had no problem following this Ruby2D tutorial up until the projectile lesson. This is the second time I've had an issue.

    I am supposed to be making a cog wheel projectile launch, but I get two NullRefs that point to the same script. The tutorial said to change the name of the Start function to Awake, but it's still happening. The projectile does get instantiated, but just sits there immobile. Everything else works, so if an enemy touches the projectile it gets destroyed.

    Tutorial: https://learn.unity.com/tutorial/wo...66dbedbc2a0021b1bc7c#5c7f8528edbc2a002053b3ed


    Pic 1 (Exception)


    Pic 2 (Script, with problem lines highlighted)


    Please let me know if anything else is needed to clarify the issue.
     
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    Did you drag the object with the RigidBody2D into the inspector? When it's public you need to manually assign it in the Inspector.

    **EDIT oh duh. Didn't even see you didn't make it public or private lol
     
    Last edited: Aug 11, 2019
  3. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    void Awake function should read:

    Code (csharp):
    1.  void Awake()
    2.  {
    3.      rigidbody2d = GetComponent<Rigidbody2D>();
    4.  }
     
    knobblez likes this.
  4. Gravano

    Gravano

    Joined:
    Jan 2, 2018
    Posts:
    16
    Awesome, I need to pay closer attention in the future.
     
  5. ed_s

    ed_s

    Unity Technologies

    Joined:
    Apr 17, 2015
    Posts:
    165
    FYI, there is a dedicated thread for the 2D Game Kit where you might find some helpful info.