Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 markzareal · Oct 25, 2014 at 04:39 AM · javascriptselection

How to make a selection system?

Hi, I'm trying to make a selection system. At the start of the game it will ask you to select(click) the character you want. If you selected it there will be a small triangle underneath the character to show that you selected it and will disappear if you selected a different character. The code is attached to all 4 of the characters. Heres the code I tried:

 #pragma strict
 var orangeSelect : GameObject;
 var pinkSelect : GameObject;
 var blueSelect : GameObject;
 var greenSelect : GameObject;
 static var orangeSelection : boolean;
 static var pinkSelection : boolean;
 static var blueSelection : boolean;
 static var greenSelection : boolean;
 
 function Start () {
     orangeSelection = true;
     orangeSelect.SetActive(true);
 }
 
 function Update () {
     if(Input.GetMouseButtonDown(0)) {
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
         if (collider.Raycast(ray, hit, 100.0)) {
             if( hit.collider.gameObject.name == "Orange")
              {
                  if(pinkSelection == true||blueSelection == true||greenSelection == true) {
                      orangeSelection = true;
                      pinkSelection = false;
                      blueSelection = false;
                      greenSelection = false;
                      BlahBlah ();
                      if(orangeSelection == true) {
                          orangeSelect.SetActive(true);
                      }
                      if(orangeSelection !== true) {
                          orangeSelect.SetActive(true);
                      }
                  }
              }
              if( hit.collider.gameObject.name == "Pink")
              {
                  if(orangeSelection == true||blueSelection == true||greenSelection == true) {
                      pinkSelection = true;
                      orangeSelection = false;
                      blueSelection = false;
                      greenSelection = false;
                      BlahBlah ();
                      if(pinkSelection == true) {
                          pinkSelect.SetActive(true);
                      }
                      if(pinkSelection !== true) {
                          pinkSelect.SetActive(true);
                      }
                  }
              }
              if( hit.collider.gameObject.name == "Blue")
              {
                  if(pinkSelection == true||orangeSelection == true||greenSelection == true) {
                      blueSelection = true;
                      pinkSelection = false;
                      orangeSelection = false;
                      greenSelection = false;
                      BlahBlah ();
                      if(blueSelection == true) {
                          blueSelect.SetActive(true);
                      }
                      if(blueSelection !== true) {
                          blueSelect.SetActive(true);
                      }
                  }
              }
              if( hit.collider.gameObject.name == "Green")
              {
                  if(pinkSelection == true||blueSelection == true||greenSelection == true) {
                      greenSelection = true;
                      pinkSelection = false;
                      blueSelection = false;
                      orangeSelection = false;
                      BlahBlah ();
                      if(greenSelection == true) {
                          greenSelect.SetActive(true);
                      }
                      if(greenSelection !== true) {
                          greenSelect.SetActive(true);
                      }
                  }
              }
         }
     }
 }
 
 function BlahBlah () {
     yield WaitForSeconds (0.1);
 }

It kind of worked but theres still some problems: 1. It only detects my click when I clicked right in the middle of the characters. 2. The triangle wouldn't disappear after I clicked a different character.

Does anyone have any ideas why this happen? I checked my script several times but I can't anything wrong with it. Thanks

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 yashpal · Oct 25, 2014 at 05:53 AM

Hello markzareal,

Here is main logical flow of solution .

1) Generate only one script which have all record of your current status. means script similar to your created.(I called this file PlayerSelectionManager)

2) make one other script for every object you need to detect touch event. (I called this file touchDetector.)

3) touchDetector file have only touch detect code like onMouseDown() and send massage to PlayerSelectionManager when onMouseDown called.

4) perform action according to that event in PlayerSelectionManager.

I am not expert in javascript so i code in c#.

1) PlayerSelectionManager

 public class PlayerSelectionManager : MonoBehaviour {
 
 
     GameObject orangeSelect ;
     GameObject pinkSelect ;
     GameObject blueSelect ;
     GameObject greenSelect ;
 //    int SelectedId;
     bool orangeSelection;
     bool pinkSelection ;
     bool blueSelection ;
     bool greenSelection ;
     // Use this for initialization
     void Start () {
     
         orangeSelection = true;
         orangeSelect.SetActive(true);
     }
 
     void ChangeSelection(string objectName)
     {
         BlahBlah ();
 
         switch(objectName)
         {
         case "Orange" :
 
             pinkSelection = false;
             blueSelection = false;
             greenSelection = false;
 
             if(orangeSelection == true) {
                 orangeSelect.SetActive(true);
             }else if(orangeSelection != true) {
                 orangeSelect.SetActive(true);
             }
 
             pinkSelect.SetActive(false);
             blueSelect.SetActive(false);
             greenSelect.SetActive(false);
 
             break;
 
             case "Pink" :
 
             orangeSelection = false;
             blueSelection = false;
             greenSelection = false;
 
             if(pinkSelection == true) {
                 pinkSelect.SetActive(true);
             }
             if(pinkSelection != true) {
                 pinkSelect.SetActive(true);
             }
 
             orangeSelect.SetActive(false);
             blueSelect.SetActive(false);
             greenSelect.SetActive(false);
 
             break;
             // And so on.
         }
 
     }
 
 
     void BlahBlah()
     {
     }
 }

2) touchDetector

 public class touchDetector : MonoBehaviour {
 
     void OnMouseDown() {
         SendMessageUpwards ("ChangeSelection", name);
 
     }
 
 }

Note. Here i use SendMessageUpwards() so every orangeSelect,pinkSelect,... are child of gameObject which have PlayerSelectionManager script attach to it. (it is not must you can use SendMessage() it is unto you.) (It is not tested)

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 markzareal · Oct 25, 2014 at 08:47 AM 0
Share

So is the touchDetector attached to anything? And do you mean that I have make an empty parent gameObject and make each of the orangeSelect, pinkSelect... child of the empty one?

avatar image yashpal · Oct 25, 2014 at 09:25 AM 0
Share

touchDetector is attached to pinkSelectObject, orangeSelectObject, .. (means object where you want to detect touch. means all players game object when you selecting them.) empty parent gameObject is have PlayerSelection$$anonymous$$anager. image

image.jpg (24.1 kB)
avatar image markzareal · Oct 25, 2014 at 09:34 AM 0
Share

Hi, thanks! You already helped me with two questions!

avatar image markzareal · Oct 25, 2014 at 09:50 AM 0
Share

by the way, in some places, it said 'objectname' or 'name' do I put the parent object's name in there?

avatar image yashpal · Oct 25, 2014 at 05:15 PM 0
Share

you don't need to replace name "objectname" it's name of your player name "Orange","Pink" GameObject in inspector. so switch case can compare to this name and detects it's clicked or not.

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

BCE0049 error with network script 0 Answers

Weird thing with my script :/ 1 Answer


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