Limiting objects allowed and fixing a score script

Hello, I am pretty new at unity and programming in general and I am currently working on a project that will involve clicking on good objects and cutting bad ones. I’ve managed to figure out make the objects move randomly and clicking to add a score, but there are still a few things that I have been unable to piece together from tutorials and other Q&A’s (or maybe I wasn’t looking hard enough or in the right places).

First, How would I go about limiting the amount of objects I have floating around? I have prefabs of eight tokens in total, but would like to limit it to only three total at a time. (even if it ends up being three of same kind at some point). I want to have the tokens disappear after being touched and then to have another one pop up at a set point.

Going off of that, I would also need a way to add the GUItext I am using for my score to the script I have on each prefab. I had the scoreObject attached on each object, but when I made them a prefab and readded them to the scene, the scoreObject was blank.

Next I need help with programming a cutting motion (click, drag, let go) and adding a particle effect to the motion so that it can be seen. (I’m guessing, I would just have an empty follow the mouse clicks and have a particle system only activate when the mouse button was pushed/held down?)

After that, the only other problem I’ve had is making the score only track one number. Right now, each object tracks its own score. Would I need one script that controls the scoreboard and then 4 more scripts for clicking-good, cutting-bad, cutting-good, and clicking-bad or would I be able to do all of that in one script that can be put all eight objects?
This is what my score script looks like right now

#pragma strict
static public var score = 0;
var scoreObject : GUIText;

function Start () {
    score = 0;
    AddScore(0);
}
function AddScore (points : int) {
    score += points;

    scoreObject.guiText.text = "Score: " + score.ToString();
}

function OnMouseDown(){
    AddScore(5);
    Destroy(gameObject);
}

Thank you for your time and I am sorry if this is too much for one question.

Sorry about that, the code I posted was in c# not javascript. Here is the code in javascript form.

public class Score
{
    private static var m_score:int = 0;
    private static var m_instance:Score;
    
    static function get Instance():Score
    {
        if(m_instance == null)
        {
            m_instance = new Score();
        }
        return m_instance;
    }
    
    static function SetPoints(points:int)
    {
        m_score = points;
    }
    
    static function AddPoints(points:int)
    {
        m_score += points;
    }
}

Just to make sure you’re not confused by all this. Here is an example of how you would use this class and perhaps even what you are trying to do.

You would add the following script to every object, whether it’s good, or bad, or cut-able. This script would (obviously just bare bones right now) handle all the logic for determining whether the object is good or bad. Then it would access the Score class to update the score. Like this:

class ClickableObject extends MonoBehaviour
{
    function Start()
    {
        //Your object's initialization.
    }
    
    function OnMouseDown()
    {
        Score.Instance.AddPoints(5);
    }
}

I’ll post a script here that works(I’m home now and could test it :])…the way it’s set up the Score script does not extend MonoBehaviour and doesn’t need ot be attached to a gameObject, it will be read from the Project folder.(Using @Thepunisher 's singleton…ty)

Here is the class named “Score”…you just need to have it inside your project

using UnityEngine;
using System.Collections;

public class Score
{
    private static Score m_instance = null;

    public int points;

    public static Score Instance
    {
        get
        {
            if (m_instance == null)
            {
                m_instance = new Score();
            }

            return m_instance;
        }
    }

    public void AddScore(int pts)
    {
        points += pts;
        Debug.Log("Current score: " + points);
    }

}

This class below need to be attached to any object in your hierarchy… it’s set up so when you press the key “F” in your keyboard it calls the function AddScore() from Score class…
and I also show the current score(variable points) on the console.

using UnityEngine;
using System.Collections;

public class TokenClass : MonoBehaviour {

	void Update () {
        if (Input.GetKeyDown(KeyCode.F))
        {
            Score.Instance.AddScore(5);
        }
	}
}

btw I code in C# but the code is simple and you can easily re-write it in javaScript…
have a go again and tell us what you got.

You should have a separate script for the score. It should be a singleton( Singleton´s Guide), so there´s only one instance of it running in your game(you don´t really want multiple scores). By the way a prefab can´t hold information of another object dragged in from the editor, so you can´t do what you were doing, it will always lose the reference.

So try that for your score class:

public class Score
{
	private static Score instance;
	public static Score GetInstance()
	{
		if (!instance)
		{
			instance = GameObject.FindObjectOfType(typeof(Score));
			if (!instance)
				Debug.LogError("There needs to be one active Score script on a GameObject in your scene.");
		}

		return instance;
	}
}

Now, to acess your score class what you simply need to call: Score.GetInstance().AddScore(X);

To help you on limiting gameObjects in the scene we need to know how you are creating them. A way to do it is to have a manager that will create them all and add each one to a list when they are created, them before creating a new one, check if there are less then 3 objects, if not dont create a new one.