How to detect the position of other gameobject?

I have 2 game objects: gameobject1 and gameobject2 and i want to check the position of gameobject2 in the script of gameobject1. I’m using go.get_position(“gameobject2”) but i’m not getting the gameobject2 position. Why is not working?

The reason for this depends, but it’s most likely caused by “gameobject2” not being the correct relative URL to gameobject2 from gameobject1. If you have a collection with two added game objects “gameobject1” and “gameobject2” then it’s enough to do go.get_position(“gameobject2”), but if the two game objects belong to different collections or one of them has another parent then the URL must be explicitly specified.

If you have a script on each of the game objects I suggest that you do:

print(msg.url())

in the init() function of each script. This will print the game objects’ absolute urls, and from those you can work out how to address them from one another. If you can’t make it work then please post the two URLs here and we’ll help you.

Please refer to the online documentation for an in-depth description of how things are addressed in Defold: http://www.defold.com/doc/message-passing#_addressing_and_urls

2 Likes