how to make score system ?

hello

I am making game about balls when player click on it turn from to yellow and red,i want to give player score when he make the balls turn to yellow and red,so i made gameobject and name it as score and wrote this script

using UnityEngine;
using System.Collections;

public class score : MonoBehaviour {
	
	
	public int scorepoint;
	
}

then i wrote this script

using UnityEngine;
using System.Collections;

public class oo : MonoBehaviour {

// put the first material here.
public Material logo;
// put the second material here.
public Material material2;
// put the third material here.
public Material material3;
	
public GameObject score;
	
	void start ()
	{
		score = GameObject.Find("score");
	}
 
bool FirstMaterial = true;
bool SecondndMaterial = false;
bool SecondndMaterial3 = false;
void Start () 
{
    renderer.material =  material2;
}
 
void OnMouseDown()
{
    if (FirstMaterial)
    {
        renderer.material = material2;
        SecondndMaterial = true;
        FirstMaterial = false;
		SecondndMaterial3 = false;
    }
 
    else if(SecondndMaterial)
{
			
			var scorescript = score.GetComponent ("score")as score ;
			
			scorescript.scorepoint += Random.Range(5,10);
			
    renderer.material = logo;
    FirstMaterial = false;
    SecondndMaterial = false;
   SecondndMaterial3 = true;
    }
		else if(SecondndMaterial3)
    {
			var scorescript = score.GetComponent ("score") as  score;
			
			scorescript.scorepoint += Random.Range(10,20);
			
        renderer.material =material3 ;
        SecondndMaterial3 = false;
		FirstMaterial = true;
        SecondndMaterial = false;
		
}
}
}

but when i play the game error message → Unassigned Reference Exception: The variable score of ‘oo’ has not been assigned.

thanks

If you want to use GameObject.find, then that should work, though it might be slow. The problem though is how you’re trying to access its member variable. Firstly, you don’t really need var here. Also, do you have the score object placed in your scene? @Clunk47 had the right approach about creating a GameObject named score, then placing the score script on that GameObject, then dragging that game object onto the score slot on oo in the Inpsector. That would remove the need to do GameObject.Find. Alas, if you want to use Find, try this to make sure the find was successful.

//Original
var scorescript = score.GetComponent("score") as score;


//You really should do
if(score != null) //implies that GameObject.Find worked
{
  //Updated
  Score scorescript = score.GetComponent<Score>(); //edited for C# correctness
  scorescript.scorePoint += Random.Range(10,20);
}

Your Start function to get the component is called start not Start so it isn’t running. Then you’ve also got a function actually called Start - I guess you should merge these two together.

If it tells you something has not been assigned, that’s because you have ‘score’ defined as a public GameObject in your ‘oo’ class. This just means you didn’t drag your object into the ‘score’ slot on your oo script in the editor. Since you’re defining ‘score’ in Start(), make it a private variable instead of public.

private GameObject score;

        //Or just GameObject score; since variables are private by default in C#

What I think you probably mean to do is create a score object in the “oo” script, rather than referencing a game object called “score”.

You could do this by changing “public GameObject score;” to:

public score score = new score();

Then remove this line:

score = GameObject.Find("score");

p.s. it’s common to name class names in CamelCase.

However, do you intent to add more functionality to the Score class? If not you could delete it completely and just declare the variable in you “oo” behaviour class.