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);
}
*/
}