- Home /
On Click, have a texture/sprite appear where I clicked showing the score value for the object clicked on
Hi Guys/Gals,
I'm currently making a game that is simply a box, where fish spawn, and you have to click on the fish to Destroy() them, and you have 30 seconds to Destroy() as many fish as possible. Each fish has a point value; 1 point, 2 points, 3 points etc.
Now, I've got all the working code for knowing which fish has been clicked, and to Destroy() it once it's been clicked.
The trouble I'm having, is where to start with adding in something like the picture I quickly whipped up below:
I've never had to make something appear other than Instantiating a particle prefab at the point of click origin, so I really have no idea where to start with making something like the image's red spiky circle appear.
I have a feeling that it may have something to do with a GUIText/3D Text to show the fishes score value, but the image of the spiky circle (or whatever it may be, an image of a bubble would be more fitting) I have no idea how to implement, so I thought I'd ask the wonderful community on UA.
Here is the actual code snippet that destroys the fish:
// Fish Destr0y Script
if (Input.GetMouseButtonDown(0))
{
// Create the touch particle effect
onTouchScore();
// Print Score to 'totalScore' GUIText
scoreText.guiText.text = totalScore.ToString();
// Logging the Mouse Position to the console
Debug.Log(Camera.main.ScreenPointToRay(Input.mousePosition));
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
// The actual destruction of the fish
if (hit.collider.gameObject.name == "fish_red(Clone)")
{
Destroy(hit.collider.gameObject.gameObject);
totalScore += 1;
Debug.Log("<color=red>KILLED A RED FISH</color>");
}
else if (hit.collider.gameObject.name == "fish_orange(Clone)")
{
Destroy(hit.collider.gameObject.gameObject);
totalScore += 2;
Debug.Log("<color=red>KILLED A ORANGE FISH</color>");
}
else if (hit.collider.gameObject.name == "fish_green(Clone)")
{
Destroy(hit.collider.gameObject.gameObject);
totalScore += 3;
Debug.Log("<color=red>KILLED A GREEN FISH</color>");
}
else if (hit.collider.gameObject.name == "fish_blue(Clone)")
{
Destroy(hit.collider.gameObject.gameObject);
totalScore += 4;
Debug.Log("<color=red>KILLED A BLUE FISH</color>");
}
else if (hit.collider.gameObject.name == "fish_purple(Clone)")
{
Destroy(hit.collider.gameObject.gameObject);
totalScore += 5;
Debug.Log("<color=red>KILLED A PURPLE FISH</color>");
}
else if (hit.collider.gameObject == null)
{
Debug.Log("You hit NOTHING!");
}
}
I hope that I don't come across needy, but this is the first time I've actually got stuck in this mini-project and don't know where to turn!
All the best!
Answer by Baste · Sep 24, 2014 at 03:58 PM
A super-easy solution is to make a bunch of different prefabs for each possible score, and instantiate them at the position of the destroyed fish.
So you'd make a prefab with the red spikey circle, "+1" inside it, and then link it to your script with a public GameObject. Then, you'd modify your code like this:
if (hit.collider.gameObject.name == "fish_red(Clone)")
{
Instantiate(onePointPrefab, hit.collider.gameObject.transform.position, Quaternion.identity);
Destroy(hit.collider.gameObject.gameObject);
totalScore += 1;
Debug.Log("<color=red>KILLED A RED FISH</color>");
}
Repeat for each possible score. You'll also want the Point prefabs to contain code that makes them go away after a while.
You sir, are legend!
This is perfect, and simple! Answer graciously accepted! Thank you ever so much. :)