- Home /
Ending a game with Points
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!!
Answer by Hellium · Oct 31, 2017 at 07:06 AM
Remove your End
script and attach the following instead :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
private int score ;
public int Score
{
get { return score ;}
set
{
if( value > 0 )
score = value ;
Debug.Log( "The new score is : " + score ) ;
if( score == 5 )
{
Debug.Log("now free to leave");
Application.LoadLevel("Menu");
}
}
}
}
Attach the following script to your Orb, and drag & drop the gameobject holding your Gamemanager
script into the gamemanager
field in the inspector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Orb : MonoBehaviour
{
public GameManager gameManager ;
void OnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter() was called "+ other.gameObject);
Debug.Log("Object is an orb");
gameManager.Score++;
Destroy(gameObject, 0.01f);
}
}
Your answer
Follow this Question
Related Questions
My score script doesn't seem to work properly, score is not added properly 0 Answers
Central score counter for 3 score generating buttons 1 Answer
INCREASE SCORE MORE AND MORE? 1 Answer
Score counter breaking after adding points 2 Answers
Can't increase my score after OnTriggerEnter occurs. 1 Answer