Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nikkimouse · Sep 21, 2015 at 09:32 AM · c#scripting problemonmousedown

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')?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nikkimouse · Sep 22, 2015 at 01:42 AM 0
Share

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?

avatar image Suddoha Nikkimouse · Sep 22, 2015 at 03:54 AM 0
Share

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();
     }
 }
avatar image Nikkimouse Suddoha · Sep 22, 2015 at 12:08 PM 1
Share

Wow! You're a life saver. Thanks ever so much, I really appreciate your help!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnMouseDown() not detecting click every frame 1 Answer

OnMouseDown and OnMouseUp not working,onMouseDown not working 0 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges