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 Hylandude · Nov 18, 2014 at 05:32 AM · not workingnot-working

Why does this script work for only one position?

I'm making a game with my team where you drag letters to their correct positions. We have a script made for the correct letters, if the letter is dragged to a certian X and Y range it is suppoed to snap into a position and remain there. In the sample scene (attached file) only one letter works (the "J") while the other correct letter (the "A") doesn't both have the exact same script and have the correct values assigned yet only one letter works. The word is supposed to spell "Abeja" (bee in spanish)

I've even tried making a new scene and assigning those values again but I get the same results only one position works and if I move that correct position it stops working.

The script receives a public Transform variable which is an empty game object and that position is where the correct letter should snap to yet it returns.

Since the sample scene exeeds the aattachment limit you can download it here(1.56MB): https://www.dropbox.com/s/3hbytcht2e9virq/TestScene.unitypackage?dl=0

Any help is highly appreciated.

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 sysameca · Nov 18, 2014 at 07:06 AM

Change your Update code like this:

     void Update()
     {
         if (Input.GetMouseButtonUp(0) && hit && movible)
         {
             if (mover.position.x <= rango.position.x + 1.5 && mover.position.x >= rango.position.x - 1.5)
             {
                 Debug.Log("Letra correcta!");
                 mover.position = rango.position;
                 movible = false;
             }
             else
             {
                 mover.position = posicion;
             }
         }
     }

You are over repeating your code by nesting it which super easy leads to this kind of bugs. My suggestion is when you see you do code like this:

 if(mover.position.x<=rango.position.x+1.5 && mover.position.x>=rango.position.x-1.5){
                 if(mover.position.y<=rango.position.y+1.5 && mover.position.x>=rango.position.y-1.5){
                     Debug.Log("Letra correcta!");
                     mover.position=rango.position;
                     movible = false;
                 }
                 else{
           
                     mover.position=posicion;
                 }
             }
             else{
                 mover.position=posicion;
             }

You need to refactor it at any cost.

Comment
Add comment · Show 1 · 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 Hylandude · Nov 18, 2014 at 12:55 PM 0
Share

Thanks a lot you really helped me. I added an extra bit since that solution only checks for a correct X range so if the height where you drag the correct letter is wrong it will still be marked as correct.

 if (Input.Get$$anonymous$$ouseButtonUp(0) && hit && movible)
         {
             if ((mover.position.x <= rango.position.x + .5 && mover.position.x >= rango.position.x - .5)&&
                 (mover.position.y <= rango.position.y+.5 && mover.position.y>= rango.position.y-.5))
             {
                 Debug.Log("Letra correcta!");
                 mover.position = rango.position;
                 movible = false;
             }
             else
             {
                 mover.position = posicion;
             }
         }

Again thanks, we are extremely happy. Thanks to you we will be able to deliver on time!

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

How to stop a Random Number Generator 1 Answer

What browser support unity (Read this please) 1 Answer

OnTriggerEnter not trigger after ios build 0 Answers

Can you fix/update the old scripts at Unity.2019.1? 0 Answers

Unity Remote 5 not working on my Nexus 6 - Marshmallow. 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