# Gameobjects return same position and are different (SOLVED)

**URL:** <https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685>\
**Category:** Questions\
**Created:** [April 18, 2019, 11:42am UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685 "2019-04-18T11:42:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DonWayo](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@DonWayo](https://forum.defold.com/u/DonWayo)\
**Post date:** [April 18, 2019, 11:42am UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/1 "2019-04-18T11:42:24Z")

</div>

I have two game objects with different urls with the same script , In the init I get the spawn pos that is correct on the console log , but appear to get the position off the other gameobject . I want to save the init  
position independent of the game object with the same script.

How I can do it?

[https://forum-defold-uploads.b-cdn.net/original/2X/b/bf0f77672d251be017fe13f5f15be511be5b57e2.mov](https://forum-defold-uploads.b-cdn.net/original/2X/b/bf0f77672d251be017fe13f5f15be511be5b57e2.mov)

![53](https://defold-forum-bucket.nbg1.your-objectstorage.com/original/2X/5/57cb14b5dbfc6b8b8e2768412eb8ce75e44393b0.png)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [April 18, 2019, 1:27pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/2 "2019-04-18T13:27:53Z")

</div>

How are you storing the position? Two game objects with the same script can store instance specific variables in the self object.

---

<div class="post-metadata">

**Author:** ![DonWayo](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@DonWayo](https://forum.defold.com/u/DonWayo)\
**Post date:** [April 18, 2019, 1:33pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/3 "2019-04-18T13:33:19Z")

</div>

```
local initPos = vmath.vector3()
       
function init(self)
    	   initPos = go.get_position()
        end

if canReturnToHand == true then
		local pos = initPos
		go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_OUTBACK, 0.2)
		go.animate(go.get_id(), "position.y", go.PLAYBACK_ONCE_FORWARD, pos.y, go.EASING_OUTBACK, 0.2) 

```

This is the way that I use to “store” the init position

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [April 18, 2019, 1:39pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/4 "2019-04-18T13:39:59Z")

</div>

You should store on self, not as a local. Local variables outside the lifecycle functions will be shared between instances.

> **[Lua programming in Defold](https://defold.com/manuals/lua/)**
>
> This manual will give a quick introduction to the basics of Lua programming in general and what you need to consider when working with Lua in Defold.

---

<div class="post-metadata">

**Author:** ![DonWayo](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@DonWayo](https://forum.defold.com/u/DonWayo)\
**Post date:** [April 18, 2019, 2:09pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/5 "2019-04-18T14:09:13Z")

</div>

If I replace with self I get this error, How I can get the init pos in a correct way?

```
attempt to index global 'self' (a nil value)

```

stack traceback:  
/main/main.script:33: in function ‘returnToOrigin’  
/main/main.script:76: in function \</main/main.script:49\>

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [April 18, 2019, 2:24pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/6 "2019-04-18T14:24:13Z")

</div>

It sounds like you tried to use it outside of a function. ‘self’ isn’t a global, it is an argument passed in to the functions called by the engine. You just need to do:

```lua
function init(self)
    self.initPos = go.get_position()
end

```

Then you can access it from inside the other basic function (update, on\_message, etc.)

---

<div class="post-metadata">

**Author:** ![DonWayo](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@DonWayo](https://forum.defold.com/u/DonWayo)\
**Post date:** [April 18, 2019, 2:34pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/7 "2019-04-18T14:34:51Z")

</div>

But I cannot access in custom functions no? outside the engine by default functions I mean.

---

<div class="post-metadata">

**Author:** ![TheKing0x9](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/theking0x9/32/13588_2.png) [@TheKing0x9](https://forum.defold.com/u/TheKing0x9)\
**Post date:** [April 18, 2019, 2:37pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/8 "2019-04-18T14:37:03Z")

</div>

you can, by passing self as an argument.

```lua
local function show(self, other_args) --notice self is an argument
--do something with self
end

function init(self)
    show(self, other_args) --self passed as an argument
end

```

---

<div class="post-metadata">

**Author:** ![DonWayo](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/d/94ad74/32.png) [@DonWayo](https://forum.defold.com/u/DonWayo)\
**Post date:** [April 18, 2019, 2:39pm UTC](https://forum.defold.com/t/gameobjects-return-same-position-and-are-different-solved/49685/9 "2019-04-18T14:39:28Z")

</div>

Okay thanks, Now I get how its working 🙂
