The code i am using following
`using UnityEngine;
using System.Collections;
public class CollisonWin : MonoBehaviour
{
GameObject goMAINMENU;
public MainMenu MM;
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Player")
{
goMAINMENU = GameObject.Find("ScoreSystemCube");
MM = goMAINMENU.gameObject.GetComponent();
MM.SaveScore();
Debug.Log("Winning!");
Application.LoadLevel("2DKemi_Level2");
}
}
}`
SaveScore
using UnityEngine;
using System.Collections;
public class ScoreSystem : MonoBehaviour {
public int score;
public GUISkin Scoreskin;
public bool saveScoreSetting;
GameObject goMAINMENU;
public MainMenu MM;
public void ToggleScore()
{
goMAINMENU = GameObject.Find("ScoreSystemCube");
MM = goMAINMENU.gameObject.GetComponent<MainMenu>();
saveScoreSetting = MM.WhatModeIsIt();
if (Application.loadedLevelName == "2DKemi_Level1")
{
if (saveScoreSetting == true)
{
//ADD points
score = 0;
}
if (saveScoreSetting == false)
{
//LOSE points
score = 500;
}
}
if (Application.loadedLevelName == "2DKemi_Level2")
{
if (saveScoreSetting == true)
{
//ADD points
score = 0;
}
if (saveScoreSetting == false)
{
//LOSE points
score = 1000;
}
}
}
public void AdjustScore(int adj)
{
if (saveScoreSetting == true)
{
if (adj < 0)//om minus poäng, sätt till 0;
{
adj = 0;
}
}
if (saveScoreSetting == false)
{
if (adj > 0)//om plus poäng, sätt till 0;
{
adj = 0;
}
}
if (score < 0)
{
score = 0;
}
else
{
score = score + adj;
}
}
void OnGUI()
{
float InfoBoxWidth = (Screen.width * 0.15f);
float InfoBoxheight = (Screen.height * 0.10f);
float scoreBarLengthLock = (Screen.width / 4);
GUI.skin = Scoreskin;
GUI.Box(new Rect(Screen.width - (scoreBarLengthLock + 10), Screen.height - 32, (InfoBoxWidth*1.8f), InfoBoxheight), " ");
GUI.Box(new Rect(Screen.width - (scoreBarLengthLock + 10), Screen.height - 32, (InfoBoxWidth*1.8f), InfoBoxheight), "Poäng: " + score);
}
void Start () {
ToggleScore(); // CHANGES SCORE SYSTEM TO POSITIVE OR NEGATIVE
}
// Update is called once per frame
void Update () {
if (score < 0)
{
score = 0;
}
}
}
MainMenu
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour {
GameObject goPLAYER;
public bool ChoosenMode;
public int[] SavedScore = new int[10]; //totalpoäng i 0, poäng för bana 1 i 1, bana 2 i 2 osv.
public GUISkin Scoreskin;
public ScoreSystem SS;
float InfoBoxwidth = (Screen.width * 0.15f);
float InfoBoxheight = (Screen.height * 0.10f);
float scoreBarLengthLock = (Screen.width / 4);
void OnGUI()
{
if (Application.loadedLevelName == "Start_Menu")
{
GUI.skin = Scoreskin;
if (GUI.Button(new Rect(((Screen.width / 2) - (InfoBoxwidth + 10)), ((Screen.height / 2) - (InfoBoxheight / 2)), InfoBoxwidth, InfoBoxheight), "+"))
{
ChoosenModeFunction(true);
Application.LoadLevel("2DKemi_Level1");
}
if (GUI.Button(new Rect(((Screen.width / 2) + 10), ((Screen.height / 2) - (InfoBoxheight / 2)), InfoBoxwidth, InfoBoxheight), "-"))
{
ChoosenModeFunction(false);
Application.LoadLevel("2DKemi_Level1");
}
}
}
void ChoosenModeFunction(bool Choosen_feedback)
{
ChoosenMode = Choosen_feedback;
print("THE CHOSEN MODE IS: " + ChoosenMode);
}
public bool WhatModeIsIt()
{
return (ChoosenMode);
}
public void SaveScore()
{
int scoreToSave;
scoreToSave = SS.score;
if (Application.loadedLevelName == "2DKemi_Level1")
{
SavedScore[1] = scoreToSave;
SavedScore[0] = SavedScore[0] + scoreToSave;
print("PLAYER GOT THIS SCORE LAST MAP: " + scoreToSave);
}
if (Application.loadedLevelName == "2DKemi_Level2")
{
SavedScore[2] = scoreToSave;
SavedScore[0] = SavedScore[0] + scoreToSave;
print("PLAYER GOT THIS SCORE LAST MAP: " + scoreToSave);
}
}
void awake()
{
//DontDestroyOnLoad(this);
DontDestroyOnLoad(transform.gameObject);
goPLAYER = GameObject.Find("First Person Controller");
SS = goPLAYER.gameObject.GetComponent<ScoreSystem>();
SS.ToggleScore();
print("THE CHOSEN MODE IS: " + ChoosenMode);
}
void Start () {
}
// Update is called once per frame
void Update () {
}
}
This is seposed to findout when the player collide whit this objektive to make it a “goal” and then load the next map.
but before this i have had a simple script in the start that chooses if the game will use positive or negative feedback.
this is then stored in that GO.
in the same file i have a “save score” kind of function that saves the games score to an array
Error:
NullReferenceException: Object reference not set to an instance of an object
MainMenu.SaveScore () (at Assets/Standard Assets/Scripts/Scripts/MainMenu.cs:55)
CollisonWin.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Standard Assets/Scripts/Scripts/CollisonWin.cs:26)
as the go does not show up, but its still active, it reconnaissances it as null
PS: c# please