Scoring system help
I want to create a scoring system so that every time a set color collides with another set color you get one point and the colorObject is destroyed i.e blue ball and a blue wall however I created the scripts for gameobject(destroy) for each color in 4 different scripts so I was wondering if it is possible that if a blue ball collides with a blue wall in the if statement I could write successfulCollision; and then in my score script I could use some kind of code to add a point for every successfulCollision;
example of one of the collision scripts: using UnityEngine; using System.Collections;
public class RedSquareCollision : $$anonymous$$onoBehaviour {
public AudioClip[] audioClip;
private string SuccessfulCollision;
void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision2D.transform.name == "RedWall")
{
PlaySound(0);
Destroy(gameObject);
SuccessfulCollision;
}
if (collision2D.transform.name == "BlueWall")
{
PlaySound(1);
}
if (collision2D.transform.name == "GreenWall")
{
PlaySound(1);
}
if (collision2D.transform.name == "YellowWall")
{
PlaySound(1);
}
}
void PlaySound(int clip)
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = audioClip[clip];
audio.Play();
}
}
Answer by ZefanS · Nov 18, 2015 at 06:38 AM
So right now it looks like your script detects when the correctly coloured ball hits the correctly coloured wall, so then you just need a script to keep track of the score, with a method that can be called to increase it. Something like:
using UnityEngine;
using System.Collections;
public class ScoreKeeper : MonoBehaviour
{
public int score;
void Start()
{
score = 0;
}
public void IncreaseScore()
{
score++;
}
}
Attach this to a score-keeping GameObject and create references to the script in each of your collision scripts. Then you can do this:
void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision2D.transform.name == "RedWall")
{
PlaySound(0);
Destroy(gameObject);
scoreKeeper.IncreaseScore();
}
//...
}
Hope this helps.
Why do I get this error? NullReferenceException: Object reference not set to an instance of an object This is my code : using UnityEngine; using System.Collections;
public class BlueSquareCollision : $$anonymous$$onoBehaviour {
public AudioClip[] audioClip;
void OnCollisionEnter2D(Collision2D collision2D)
{
if (collision2D.transform.name == "BlueWall")
{
ScoreSystem scoreSystem = GetComponent<ScoreSystem>();
scoreSystem.IncreaseScore();
Destroy(gameObject);
}
if (collision2D.transform.name == "RedWall")
{
PlaySound(1);
}
if (collision2D.transform.name == "GreenWall")
{
PlaySound(1);
}
if (collision2D.transform.name == "YellowWall")
{
PlaySound(1);
}
}
void PlaySound(int clip)
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = audioClip[clip];
audio.Play();
}
}
In general that error means that the script can't find the Object being referenced to. For instance, if my script had something like:
public GameObject myGameObject;
myGameObject.GetComponent<$$anonymous$$yScript>().$$anonymous$$y$$anonymous$$ethod();
And I forgot to assign a GameObject to myGameObject in the Inspector, I would get that NullReferenceException error.
What line does it list as the source of the error? I'm going to guess that
ScoreSystem scoreSystem = GetComponent<ScoreSystem>();
isn't working correctly. Did you attach the ScoreSystem script to the same GameObject as the BlueSquareCollision script? If not, you need to specifiy which GameObject you are getting the component from.
ScoreSystem scoreSystem = myGameObject.GetComponent<ScoreSystem>();
and assign myGameObject as a variable in the Inspector or otherwise get a reference to it.
Or do something like this in-line:
ScoreSystem scoreSystem = GameObject.Find("$$anonymous$$yScoreGameObject").GetComponent<ScoreSystem>();