Using Points To End A Game

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!!

I think the issue is -

    public CollectOrb orbs;

this declares a member variable to hold a reference to the CollectOrb gamer object, but you never store anything in it.

Perhaps try adding something like this to the Start() method of the End class

        orbs = GetComponentInParent<CollectOrb>();

I’m new to Unity myself so the above may not be the best solution

Hello mpina!

You could do everything in one single script (attached to the player), which would be much more easier. It could look like this:

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

public class WinCondition : MonoBehaviour
{
    int Score;

    void Update ()
    {
        //If the player has collected all the orbs
        if (Score == 6)
        {
            //Loads the scene
            SceneManager.LoadScene("NameOfYourScene", LoadSceneMode.Single);
        }
    }

    private void OnTriggerEnter (Collider orb)
    {
        //Whenever your player collect an orb
        if (orb.CompareTag("Orb"))
        {
            //Informs the player he just collected an orb
            Debug.Log("You've just collected an orb!");
            //Then we destroy the orb
            Destroy(orb.gameObject);
            //Then we increment the score
            Score++;
        }
    }
}

Simply attach this script to your player! A few things to do after that: create a new tag (named Orb) and assign this tag to all your orbs. Finally, make sure all your orbs have a Collider (which I’m sure it’s already done) :slight_smile:

I think the previous post could be improved, as a good habit… something I see quite often here (* Running update checks on something that doesn’t need it…) :slight_smile:

Check for your win condition when it’s applicable; in this script’s case, that’s inside the OnTriggerEnter + correct tag comparison.

Since OP is new to Unity, I figured it would be good for OP to see how Update operates. But you’re right, checking the condition directly inside OnTriggerEnter is better. :slight_smile:

I know, I know… always “depends” and often “not harmful, either way” lol
Now the OP has two perfectly viable options and considerations! :slight_smile: