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 jeffreywarf · Nov 07, 2014 at 03:00 AM · transformpositiontagdebug

Unity thinks two objects are in same place but they'

Attach the following code to an object, then put the object with the tag "Player" anywhere else as long as the co-ordinates show as different from the position of the object with the script on it.

Now, when you hit "play", it will tell you the objects are in the same position. Why is this? This happens to me every time.

 #pragma strict
 
 var player : GameObject;
 var spawner : GameObject;
 var player_position_x : float;
 var player_position_y : float;
 
 function Start () {
 
 player = GameObject.FindGameObjectWithTag("Player");
 
 }
 
 function Update () {
 
 player_position_x = player.transform.position.x;
 player_position_y = player.transform.position.y;
 Debug.Log("X: " + player.transform.position.x.ToString() + ", Y: " + player.transform.position.y.ToString());
 Debug.Log("X2: " + transform.position.x.ToString() + ", Y2: " + transform.position.y.ToString());
 
     if(transform.position.x == player_position_x && transform.position.y == player_position_y);
     {
     Debug.Log("SamePosition");
     //player.GetComponent(PlayerScript).collected++;
         for(var more : int; more < player.GetComponent(PlayerScript).collected; more++)
         {
         spawner.GetComponent(RandGeneration).TestObj(gameObject);
         }
         //Destroy(gameObject);
     }
 
 }

even the debug information says that the x and y values are different, so why does it also believe they are the same? I've tried this in a scene devoid of any other scripts and get the same results over and over again.

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
4
Best Answer

Answer by Uldeim · Nov 07, 2014 at 03:20 AM

You have a semi-colon at the end of your if. Remove it and you're good. ;)

I've done that before.

For those who aren't aware, the semi-colon after the closing parenthesis of the if terminates the line (and therefore the IF statement!). The following bracket is therefore not actually in the if condition. Groups of bracketed code on their own are perfectly legal, so this doesn't throw a warning or an error, but it is basically never what you want.

Comment
Add comment · Show 3 · 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 jeffreywarf · Nov 07, 2014 at 03:42 AM 0
Share

wow. just. wow. XD I feel so bad, been coding for years and I still make rookie mistakes!

avatar image Uldeim · Nov 07, 2014 at 03:44 AM 0
Share

So do we all, jeffreywarf, so do we all. :(

avatar image FairGamesProductions · Nov 07, 2014 at 09:45 AM 0
Share

Oh, and in JS, you don't have to use ToString() in Debug. JS does that for you automatically.

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

27 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

Related Questions

Grab position from instantiated object 1 Answer

transforming position 1 Answer

How can I defer the recalculation of child transforms whilst modifying the parent transform? 0 Answers

How to only instantiate objects once? 1 Answer

Keeping an Object Locked In Position Around Another 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