Hey Guys,
I’m pretty new to Unity and need some help. So I’m creating a game where the player has to collect Orbs in order to go through this wall of light and end the game. They need to collect at least 6 orbs in order to finish the game and get transported back to the main menu.
Here’s my script for the ending:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class End : MonoBehaviour
{
public CollectOrb orbs;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter() was called " + other.gameObject);
if (orbs.score == 1) //testing with one orb
{
Debug.Log("now free to leave");
Application.LoadLevel("Menu");
}
else
{
Debug.Log("Keep Movin' pal");
}
}
}
Basically, I need help between making the two scripts (End and CollectOrb) communicate with each other. I thought I did it right by having the orb script call upon score, but every time the game gets to that if statement it prints “NullReferenceException: Object reference not set to an instance of an object
End.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Our Scripts/End.cs:24)”
Here’s CollectOrb as well:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectOrb : MonoBehaviour {
public int score = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter() was called "+ other.gameObject);
Debug.Log("Object is an orb");
Destroy(gameObject, 0.01f);
score++;
Debug.Log("Score is now " + score);
}
}
Thank you so much for your time!!