0
\$\begingroup\$

I'm making a small game for my English class, and in the beginning, you have to collect rocks. I already have the code for counting how many rocks have been picked up, and the GUI to display while the game runs. After the player collects all 9 rocks I would like for the GUI to disappear or delete itself since it won't be used anymore throughout the game, and for new text to appear at the top of the screen saying "Go To The Lottery"

I'm still very new to Unity so posting the code, and then following up with a brief explanation as to what does what would be majorly appreciated (but not at all demanded)

Both codes are in JavaScript

#pragma strict

static var currentScore : int = 0;
var offsetY : float = 40;
var sizeX : float = 100; 
var SizeY :float = 40;

function OnGUI () {
GUI.Box (new Rect (Screen.width/2-sizeX, offsetY, sizeX, SizeY), "Rocks Collected: " + currentScore);


}

^^ This is my GameMaster script, which is in a empty game object galled "_GM" ^^

#pragma strict

function OnTriggerEnter (info : Collider) 
{
    if (info.name == "Player")
    {
    GameMaster.currentScore += 1;
    Debug.Log ("Picked stone up");
    Destroy (gameObject);
    }
}

^^ This is my RockCollection script and it's what actually tells the GameMaster script to add 1 to the score and then delete the rock so you can't continuously pick it up

\$\endgroup\$
0
0
\$\begingroup\$

Your GameMaster script needs to be made aware of how many rocks the player needs to collect in order to progress. The simple solution would be to simply hard-code the targetScore to 9 and then check whether or not it is reached in your OnGui function:

#pragma strict

static var currentScore : int = 0;
static var targetScore : int = 9;
var offsetY : float = 40;
var sizeX : float = 100; 
var SizeY :float = 40;

function OnGUI () {
    var text : String;
    if (currentScore < targetScore) {
         text = "Rocks Collected: " + currentScore;
    } else {             
         text = "Go To The Lottery";
    }
    GUI.Box (new Rect (Screen.width/2-sizeX, offsetY, sizeX, SizeY), text);

}

A better solution would be to initialize the targetScore to 0 and have each rock increment it in their Start function. That way your target score is automatically set to the number of rocks you have in the scene. Just keep in mind that when you restart the game you need to reset both the currentScore and the targetScore to 0.

This is how this start function of your Rock script would look

function Start() {
    GameMaster.targetScore += 1;
}
\$\endgroup\$
0
0
\$\begingroup\$

You change which text the GUI box will display. i don't know whats your code but it should be something like this

if (rocks = 9)
  text = "Go To The Lottery"
else
  text = rocks
\$\endgroup\$
0
0
\$\begingroup\$

I have followed the Roll-a-ball tutorial from Unity a while ago and I remember displaying a 'winning text' whenever I would collect all cubes.

In my player controller I had a method for the actual pickup which would also call SetCountText() upon picking up a cube.

void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
        if (count >= 17)
        {
            winText.text = "You Win! Yayy";
        }
    }

As you can see, I would display the actual score and if I would collect all objects (17) the win text would be set.

You could do something similar but then have whatever you want to achieve, happen inside the if statement.

\$\endgroup\$
3
  • \$\begingroup\$ Would I be able to just add this onto my PlayerController script? Or would this go on to the GameMaster script to check if player has collected x amount of rocks? \$\endgroup\$ – Chris Soares Oct 23 '17 at 12:00
  • \$\begingroup\$ If you already had a place where you count the number of collected rocks, you might want to put it there. In the tutorial we placed it in the player controller, as there was not much else besides that and some rotation scripts. Note, in my case the player (a ball) was counting the collected items itself, so thats why I had the victory check in there. I will refine this comment later as i see you've uploaded code, and I am on my phone at the moment. \$\endgroup\$ – D.Kallan Oct 23 '17 at 12:38
  • \$\begingroup\$ Oke so I see in your code that you are actually setting the score in your GM from within the RockCollection script. I don't know if that is the correct way; creating a method to add something to that variable seems more pretty than actually setting it from another class. Right now, you can only add a if(count >= x)in your RockCollection script, as that is where you increase the score (GameMaster.targetScore += 1;) If you set the score from outside the class, you dont have an entry to add the if inside that class (as it is doing nothing). \$\endgroup\$ – D.Kallan Oct 23 '17 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.