- Home /
Ways to make OnMouseDown (or a single script) differentiate between different colliders/sprites
I have a world map which I want the player to be able to click on the different countries and have it flip an appropriate bool. Here's a quick example of what I've already designed, using colliders and the mouse button.
void OnMouseDown() {
canadaselected = true;
GetComponent<SpriteRenderer> ().color = Color.red;
}
The problem is that the current way I've done it means I need to write a separate script for each country and attach it to each individual sprite, which would mean nearly 200 short, individual but almost identical scripts (the only line different would be the name of the bool eg. ukselected, brazilselected, chinaselected).
Is there any other way to do this? One that could compress it into a single script I could attach to all the sprites instead of writing separate scripts? Or maybe a way that OnMouseDown could know which sprite it was over and flip the appropriate bool (for example if the bool is canadaselected and the sprite is named 'canada')?
Answer by Suddoha · Sep 21, 2015 at 10:06 PM
There is no need to write 200 almost identical scripts. You'll just reuse a generalized script, which has a boolean named "selected".
In OnMouseDown, you can set the boolean to true and access all the information that you need, such as the gameObject that this script is attached to, its children, scripts, colliders, renderers, sprites, tag, name etc.
Thanks for replying.
I did think about doing that, but I think there's going to be a problem at the next stage. I haven't written the code yet, but eventually clicking on the country is going to activate/instantiate a UI panel. Said panel will have some slight differences that I had intended to control via the different booleans, for example the country name string at the top of the panel. I've very roughly sketched out something like that below;
public void Countrydata () {
if (canadaselected)
{countryname.text = "Canada";
countrylanguage.text = "English & French";}
if (ukselected)
{countryname.text = "The United $$anonymous$$ingdom";
countrylanguage.text = "English";}
}
If I have a singular boolean, won't that mean I have to add individual panels for each country as child-objects of the country-sprite ins$$anonymous$$d of just varying the data strings/textboxes, and therefore overly-inflate the size of the game?
No, it won't inflate your game at all for the following reason: You either extend the script with public fields for your information or write a seperate one which holds the information and communicate between them.
The public fields will then allow you to simply edit the countryName and languages in the inspector on the respective country sprite.
Consider the following example, I just made everything in one script so it's easier to follow, you can actually ignore the FormatLanguages method as it will only build a string with the format 'lang1 & lang2 & lang3' and so on.
The text component represents your UI object, i just used a text for the demo script
public class Example : $$anonymous$$onoBehaviour {
public string countryName;
public string[] countryLanguages;
public Text countryInformation;
private const string infoTag = "infoTag";
private bool selected;
void Awake()
{
// either find the Text component at runtime just like the following line does
// or assign it via the inspector, i left out the null-checks so that it's a bit shorter
countryInformation = GameObject.FindGameObjectWithTag(infoTag).GetComponent<Text>();
}
void On$$anonymous$$ouseDown()
{
selected = true;
// do whatever has to be done here
// I'll write the information to a simple text component
countryInformation.text = string.Format("Name: {0},\nLanguages: {1}", countryName, FormatLanguages());
}
private string FormatLanguages()
{
StringBuilder sb = new StringBuilder();
for(int i=0; i<countryLanguages.Length ; i++)
{
if (i != 0)
sb.Append(" & ");
sb.Append(countryLanguages[i]);
}
return sb.ToString();
}
}
Wow! You're a life saver. Thanks ever so much, I really appreciate your help!