Scoring without FPS Or timer

Hello Im trying to make a small Unity game in which the player explores a large free roam world in search of "Coins", Where each coin that collides with the First Person Controller will add to the score in a GUIText. I have the window set up to show my score but i do not know how to link all the components so that the score will appear in the Window. I tried using this code. . .

var windowRect : Rect = Rect (20, 20, 120, 50);
function OnGUI () {
    windowRect = GUI.Window (0, windowRect, WindowFunction, "Score");
}
currentScore.ToString();
static var currentScore : int = 0;
function WindowFunction (windowID : int) {
// convert the variable scoreNumber which is an integer to a string

// set this to the text value of your guiText to display itguiText.text = currentScore;

// A function specifically for detecting collisions with colliders 
// that have had the Is Trigger flag set in the inspector
function OnTriggerEnter(collisionInfo : Collider){
// if the current gameObject of the collider stored in the collisionInfo
// variable has a tag of "battery" (ie, we have hit a battery)
if(collisionInfo.gameObject.tag == "Coin"){            
// Increment the global score variable declared in the by 1 
// ++ is short hand for currentScore = currentScore + 1;
currentScore++;            
// Play a sound to confirm we have picked up a battery
// Remove this particular battery from the scene (the object whose collider we have hit)
Destroy(collisionInfo.gameObject); 
}    
}
}

And im guessing several of you could point out the problems in my script. I am relatively new to Unity and Java. I appreciate any help.

A friendly reminder: Format your code before posting, it'll yield better answers as people take the effort to actually read your code that way :)

–

I think Statement borked your code in formatting it. Try reposting it.

–

1 Answer

1
    var windowRect : Rect = Rect (20, 20, 120, 50);
    var currentScore : int = 0;
    var Tag = "Coin";

    function OnGUI () {
        windowRect = GUI.Window (0, windowRect, WindowFunction, "Score: " + currentScore);
    }

    function WindowFunction (windowID : int) {
        currentScore.ToString();
    }

    function OnTriggerEnter(collisionInfo : Collider){
    if(collisionInfo.gameObject.tag == Tag){
        print("collided");          
        currentScore ++;
        print(currentScore);         
        // Play a sound to confirm we have picked up a collectable
        Destroy(collisionInfo.gameObject); 
        }    
    }

hope this helps, I've just tidied up your code and reorganized it to make a bit more sense. with these changes I got it to work for myself so hopefully you can get it to work

Scribe

In your script you are asking it to do the stuff if the collectable object is tagged "Coin" you must put this script on your character and change the tag of the collectable item to "Coin". On the coin make sure you have a collider which has the is Trigger box ticked. sorry for the lack of explanation

–