x


GameObject position and localPosition not changing in hiearchy, only in script.

I have a prefab i instantiate, and when i change its position and local position to 0,0 it doesn't seem to change in hierarchy view in editor. I have a debug system that prints out values to a gui text object and it says the local position before and after is 0,0. But in the editor it is a random huge value. Why would this be?

I'm using this code to change its positions:

    debugGText.permDebug("pos: (" + transform.position.x + "," + transform.position.y + ")");
    transform.position = Vector3.zero;
    debugGText.permDebug("pos(after): (" + transform.position.x + "," + transform.position.y + ") and localPos: (" + transform.localPosition.x + "," + transform.localPosition.y + ")");
    transform.localPosition.Set(0,0,0);
    debugGText.permDebug("localPos: (" + transform.localPosition.x + "," + transform.localPosition.y + ")");

This code works fine with no compile errors and print sout the location to the gui text object. Here is a screenshot of this in the editor window(I highlighted notable parts): There are no null reference exceptions and the script is on the gameobject i wnat to modify. Notice that the gameObjects's transform only has localPosition and not normal position. Why is this? It is not a child of another object....Just a prefab. Image screencap of editor

builderPosition.png (322.5 kB)
more ▼

asked May 22 '12 at 10:30 PM

1337GameDev gravatar image

1337GameDev
526 43 49 55

My guess would be that something is changing it after your debug logging - where is that happening at the moment?

May 22 '12 at 11:14 PM whydoidoit

This script that's printing values, which object is it attached to?

  • The values you're printing refer to whatever object the script is attached to.
  • The values in the inspector refer to whatever object you have selected in the editor.

If those two aren't the same object, confusion might result.

May 22 '12 at 11:59 PM rutter

Nope. I don't modify the position of the gameObject anywhere else.... Here is my scripts on http://justPaste.it.

http://justpaste.it/108g

May 22 '12 at 11:59 PM 1337GameDev

Hmmm, well your transform.localPosition.Set(0,0,0) probably isn't doing anything as transform.localPosition returns a copy of its position I believe. But clearly the position appears to be Vector3.zero in any case. Which makes sense as it has no parent right?

May 23 '12 at 12:08 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In you full script you have this code:

 private void updateTextPos()
    {
        Vector3 worldLocation = mainCam.ScreenToWorldPoint(buildGuiLocation);
        labelOfBuilder.transform.position = worldLocation;
    }

But labelOfBuilder is a component on the same game object as the Builder script, so it moves the builder to that location, hence your changed coordinates.

more ▼

answered May 23 '12 at 12:12 AM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

Nice catch! how did i forget that?? I forgot to attach it to a child gameObject for this part of my implimentation.

May 23 '12 at 12:16 AM 1337GameDev

Just one of those - when you've exhausted the obvious and you don't know the code you see it with different eyes :)

May 23 '12 at 12:44 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1684
x1284
x1260
x890
x32

asked: May 22 '12 at 10:30 PM

Seen: 1602 times

Last Updated: May 23 '12 at 12:44 AM