Calling GetComponent<>() is detaching object at runtime

I’m working through the Angry Meteor tutorial, and it’s all working except the game Resetter Script that is attached to the Boundary that surrounds the play area (BoxCollider2D).

I dragged my Asteroid object into the RigidBody2D box for the script reference and pressed play, but I kept getting NullReference errors in Resetter script, and I saw the object disappear from the script reference box. So when I commented out GetComponent<>(RigidBody2D) it didn’t disappear from the script ref. box. So I added a debug line on the GetComponent call, and it’s null at runtime and I can’t figure out why. I tried turning off all other scripts and verified the Asteroid has a RigidBody2D,and also verified that I set the script reference back to None, and dragged it on again. It just keeps detaching. Can anyone help me out?
Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Resetter : MonoBehaviour {

    public Rigidbody2D asteroid;
    public float resetSpeed = 0.025f;

    private SpringJoint2D spring;
    private float resetSpeedSqr;

   
    void Start () {
        Debug.Log(GetComponent<Rigidbody2D>() == null);
        //asteroid = GetComponent<Rigidbody2D>();
        //spring = asteroid.GetComponent<SpringJoint2D>();
        //resetSpeedSqr = resetSpeed * resetSpeed;

    }

    /*
    void Update () {
        Debug.Log("Update");
        if (Input.GetKeyDown (KeyCode.R)) {
            Debug.Log("R Pressed.");
            Reset();
        }
        if(asteroid.velocity.sqrMagnitude < resetSpeedSqr && spring == null) {
            Reset();
        }
    }

    private void OnTriggerExit2D(Collider2D collision) {
        Rigidbody2D rb = collision.GetComponent<Rigidbody2D>();
        if (rb == asteroid) {
            Reset();
        }
    }

    void Reset() {
        Application.LoadLevel(Application.loadedLevel);
    }
    */
}

If the Rigidbody2D is supposed to be set via inspector, the line “asteroid = GetComponent” does not make sense, since it would overwrite whatever you assign to “asteroid” in edit-mode.

Have you tried simply removing that line and leaving everything else as they were?

1 Like

Yes, when I remove that line, it doesn’t disappear from the inspector.
But the script is attached to the “Boundary” objects, which takes a public RigidBody2D (the Asteroid).
Are you saying I don’t need a GetComponent to get a reference to the Asteroid?

mikael_juhala You were right.
I just took the GetComponent out and accessed asteroid. Thank you!!!

Working code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Resetter : MonoBehaviour {

    public Rigidbody2D asteroid;
    public float resetSpeed = 0.025f;

    private SpringJoint2D spring;
    private float resetSpeedSqr;

   
    void Start () {
        spring = asteroid.GetComponent<SpringJoint2D>();
        resetSpeedSqr = resetSpeed * resetSpeed;
    }
   
    void Update () {
       
        if (Input.GetKeyDown (KeyCode.R)) {
            Reset();
        }
      
        if(asteroid.velocity.sqrMagnitude < resetSpeedSqr && spring == null) {
            Reset();
        }
    
    }   

    private void OnTriggerExit2D(Collider2D collision) {       
        Reset();
    }
   
    void Reset() {
        SceneManager.LoadScene("Main");
    }
}