So I just started Unity a few months ago for school and i’ve run into a bit of a problem for my final project that i’m sure has an easy fix but I can’t think of it. For a little background we had to take our existing Apple Catch game and turn it into a top-down shooter, where the objects come at you and you shoot them down. Now I originally had the scoring script tied to my catching object, since when it collided with an object it ate the object and when that happened the score went up. But now I need to have the collision and deletion script on my “bullet” i’m shooting. The snag i’ve ran into is the “bullet” prefab won’t let me put the GUItext on it. So naturally I tried to make another script that would add to the score whenever it noticed my Apple prefab was destroyed, but I wasn’t able to work the kinks out of that either. So now i’m posting on the forums here to try and see if anyone can give me a solution or at least a start of one, i’m currently using C#. Also sorry but I don’t even know what sort of script to put on here for you guys to see so you can base it off something. I’ll post one of them is you ask.
It’s rather hard to help you without a clue what you’ve got already. However, here’s what I’d do to make a scoring system, including showing the score above the thing that generated the points:
First, I’d have a GameObject that was just for showing the score. It would update the totals onscreen, and hold the current score. It would have function that let other scripts add or subtract from the score. I’d name it something unique, like ScoreManager, so it was easy to find.
Then I’d create a prefab of a GameObject that shows the points earned at its location on-screen. Maybe call it ScorePopup.
Then I’d add a script to my bullet that adds the points to the ScoreManager, and also spawns the prefab of the ScorePopup at its current location, with the appropriate points to show, and then destroys the bullet.
I hope this helps set you on the right path.
Well, i agree with most of what wccrawford has said.
But the way i would do it is slightly different. (This way would be using 'Prefab Shooting"
- Create an empty game object, and add a GUI script that shows your score using OnGUI label function.
- Create a “Bullet” prefab that has the collision.
- Add a collision detection to the script, and add functions so that when it hit’s the “apple” and is destroyed, it will send a message to your score script and add the desired points.
If you need any more help inbox me and i can go about helping you further.
On your game scene have an empty game object (call it ‘scorecontroller’) that you can put the GUItext on (make it a child object)
Put a script on this game object as well to handle the score (Score.cs).
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour
{
public int score; // players score
public GUIText scoreGUI; // guitext object for score
// Use this for initialization
void Start ()
{
score = 0; // starting score
}
// Update is called once per frame
void Update ()
{
scoreGUI.text = "Score : "+score.ToString(); // display score guitext
}
}
Now on your bullet prefab have a script to detect the collision and add to the score (bullet.cs)
using UnityEngine;
using System.Collections;
public class bullet : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.name == "apple")
{
GameObject control = GameObject.Find("scorecontroller"); // get game system object
Score gc = control.GetComponent<Score>(); // get the script Score.cs from the object
gc.score+=10; // add 10 to players score
Destroy(col.gameObject); // destroy apple game object
}
}
}
You will have to tweak this to fit in with how you have colliders setup (is trigger or not) and for your own ‘apple’ name.
Something like this anyway.