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 :)
– StatementI think Statement borked your code in formatting it. Try reposting it.
– anon87535109