Collecting System

So I made a collecting system, but the problem is that it’s only counting 1 time (so if I collect 1 cookie it adds 1 to my counter but if I pick up another one nothing is happening).

public var myCookies : int;
public var CookieCounter : GUIText;
function OnTriggerEnter () {
(Debug.Log)("CoinCounter");
Destroy(gameObject);
myCookies += 1;
CookieCounter.text = "" + myCookies;
}

You are destroying the game object that the script is attached to, is that what you want? If you do, you should change a variable on another object’s script.

I want that if this game object is destroyed that the GUIText is turning from 0 to 1, this is working but if I now destroy another Collectible it’s not working.

Right now you are increasing the variable on the component, but also destroying this same component. So your data gets lost.

What you should do is make a manager class which keeps track of the amount of cookies. This manager class will exist only once in the scene and should be kept in the scene untill you dont need the stats anymore.

So, your manager would look like this: (in C#)

public class CookieManager : MonoBehaviour {
    private static int myCookies;
    private static GUIText CookieCounter

    public void AddCookie() {
        myCookies++;
        CookieCounter.text = myCookies.ToString();
    }

    public int GetCookieCount() {
        return myCookies;
    }
}

Let me explain the content. You assign the 2 variables, but mark them as static. This means that these variables are not instanced, but only exist 1 time for all class instances (you dont need this variable seperately for every instance). Then in the AddCookie function, you increase the numer of cookies and update the GUIText. By calling the GetCookieCount function, you can get the number of cookies as an int via scripting

Then you create a class for every cookie, like you already did. But then, in OnTriggerEnter, you get the CookieManager class by it’s name for example, and call the AddCookie() method. After that, destroy the gameObject like you did. You can get rid of the rest in that script.

Hope this helpes, if it is all a bit overwhelming I’ll try to explain it a bit more indepth, as you seem to have just started with programming

Yes, I’m really to programming :). It helped me, although I’m not understanding all of your text. But very much thanks from me, for this explaination.