how to add score for apple catch game c#

Hi all, (newbie here)

I’m making an apple catch game and trying to figure out how to add the score for it when my basket catches the apple. I’ve been looking through several tutorials online but there isn’t any that is specifically made to score apples being caught. If someone can guide me through it step by step with the script for the guitext that’d be very helpful…

thanks

Hey,

Firstly you need to make a GUIText object in your game to do this you need to go to, GameObject, Create other, GUI Text. Position this how you want in the transformer. Note that it is only visible in the game view. also you can add a font, change the size etc in the GUI Text component of the new GameObject.

Then make a new script and declare an int variable like this:

private int Score = 0;

I’m going to assume that when the apple hit the basket you are using the OnCollisionEnter if not add it to your script like so:

void OnCollisionEnter(Collision col) {

}

Now the variable col contains the data of the collision, in our case the apple with the basket. But now it doesn’t do anything, let’s change that.

Create the tag Apple by going to the apple GameObject click tag and then on Add tag. You can Add tags them by filling them in in the element field. Then select the apple again and click on tag and select Apple.

Then add the following to the script inside the OnCollisionEnter:

If (col.GameObject.Tag == Apple){

int score ++;
Destroy (col.GameObject);
}

Here we are increasing the int score by one and destroying the GameObject Apple.

Now declare a update function (It should be there when you make a new script, but just in case). And put this in the function:

Textscore = Score.ToString();
TextObject.text = (Textscore);

Finally declare the variables for the above lines, like this:

private string Textscore;
public GUIText TextObject;

Then you only need to go to your script and drop in the GUI Text gameobject, we made in the start, from the hierarchy into the newly created script for the basket into it’s appropriate field in our case TextObject.

Of course you need to assign this script to the basket first.

If something isn’t clear or won’t work just ask in the comments!

EDIT: also make sure that your basket and the Apple have a collider, to add this select the Apple and in the inspector click on add component and search up Box collider, add this and to the same for the basket.

Hope this helps!

Gijs

yeah it really isnt working still. can i send you my files and have you take a quick look at it?