Hello all! I’m currently having a problem with my code that is disallowing me from playing my currently set scene, named Level1, and instead returning with a “NullReferenceException: Object reference not set to an instance of an object” error in the console window while automatically switching to Level2 and messing up a bunch of the GameObjects.
Currently my code is reading the following:
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class Enemy : MonoBehaviour
{
[SerializeField] public GameObject _cloudParticlePrefab;
public AudioClip pop;
AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void OnCollisionEnter2D(Collision2D collision)
{
Bird bird = collision.collider.GetComponent<Bird>();
if (bird != null)
{
audioSource.clip = pop;
audioSource.PlayOneShot(pop, 0.7F);
Instantiate(_cloudParticlePrefab, transform.position, Quaternion.identity);
Destroy(gameObject, 0.1F);
return;
}
Enemy enemy = collision.collider.GetComponent<Enemy>();
if (enemy != null)
{
return;
}
if (collision.contacts[0].normal.y < -0.5)
{
audioSource.clip = pop;
audioSource.PlayOneShot(pop, 0.7F);
Instantiate(_cloudParticlePrefab, transform.position, Quaternion.identity);
Destroy(gameObject, 0.1F);
return;
}
}
}
Bird.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Bird : MonoBehaviour
{
Vector3 _initialPosition;
private bool _birdWasLaunched;
private float _timeSittingAround;
[SerializeField] private float _launchPower = 350;
private void Awake()
{
_initialPosition = transform.position;
}
private void Update()
{
GetComponent<LineRenderer>().SetPosition(0, transform.position);
GetComponent<LineRenderer>().SetPosition(1, _initialPosition);
if (_birdWasLaunched &&
GetComponent<Rigidbody2D>().velocity.magnitude <= 0.1)
{
_timeSittingAround += Time.deltaTime;
}
if (transform.position.y > 10 ||
transform.position.y < -10 ||
transform.position.x > 20 ||
transform.position.x < -20 ||
_timeSittingAround > 3)
{
string currentSceneName = SceneManager.GetActiveScene().name;
SceneManager.LoadScene(currentSceneName);
}
}
private void OnMouseDown()
{
GetComponent<SpriteRenderer>().color = Color.red;
GetComponent<LineRenderer>().enabled = true;
}
private void OnMouseUp()
{
GetComponent<SpriteRenderer>().color = Color.white;
Vector2 directionToInitialPosition = _initialPosition - transform.position;
GetComponent<Rigidbody2D>().AddForce(directionToInitialPosition * _launchPower);
GetComponent<Rigidbody2D>().gravityScale = 1;
_birdWasLaunched = true;
GetComponent<LineRenderer>().enabled = false;
}
private void OnMouseDrag()
{
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(newPosition.x, newPosition.y);
}
}
LevelController.cs
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelController : MonoBehaviour
{
public static int _nextLevelIndex = 1;
public Enemy[] _enemies;
public void onEnable()
{
_enemies = FindObjectsOfType<Enemy>();
}
// Update is called once per frame
void Update()
{
foreach(Enemy enemy in _enemies)
{
if (enemy != null)
return;
}
Debug.Log("You killed all enemies!");
_nextLevelIndex++;
string nextLevelName = "Level" + _nextLevelIndex;
SceneManager.LoadScene(nextLevelName);
}
}
The LevelController.cs file at line 17 is where I’m having the troubles. The error reads in the console like this…
LevelController.Update () (at Assets/LevelController.cs:17)```
I guess the real question here is, can y'all help me identify where the problem is in the code and help me correct it?
Thank you!
P.S. I think it's worth saying that the function to switch scenes is working just fine, it's the error code and the timing of the switching of scenes that gets me. Also, I have correctly added all the scripts to their respective GameObjects, so that isn't the problem.