Pookie
Pookie

Reputation: 1279

Unity Transform position isn't changing position

I am trying to change the position of a border to be just off the top of the screen. I did this by taking the top of the screen, then adding half the scale of the object (since it is centered in the middle). However, when I do this nothing changes at all. I printed out the coordinates and it says it moved, but it didn't. Here is my code (from the Start() method in WallScalar class).

Note: The following code is the only code in the WallScalar class, this script is applied to the "Top" gameObject.

      transform.position = 
      new Vector3(0, Camera.main.orthographicSize + (transform.localScale.y / 2), 1);

      print("yCoor: " + transform.position.y);

Since the object has a scale of one, and the top is at y = 5, the object should be placed at y = 5.5f. With that print statement, it says it is at y = 5.5f but it didn't move on my screen. Here is an image for more details:

enter image description here

As you can see, it says it was moved to yCoor = 5.5f, but it didnt, because when I manually type 5.5 for y, it isn't visible, it is just off the screen. Any help with figuring this out would be greatly appreciated.

Update: If i put a print statement such as

      print("yCoorU: " + transform.position.y);

In my update method, it reads y = 5, so something is forcing it back to be 5, but know that nothing else has any call to the "Top" GameObject that the script is being applied to.

Upvotes: 0

Views: 4754

Answers (3)

Chirag Shah
Chirag Shah

Reputation: 31

So, what I figured out is that you have to disable the object you are trying to change position on, set the position and than enable it.

Player.SetActive(false);
Player.transform.position = new Vector3(PositionX, PositionY, PositionZ);
Player.SetActive(true);

This might be because during gameplay the player or the object you are trying to move is in static mode.

Chirag Shah

Upvotes: 3

Uri Popov
Uri Popov

Reputation: 2167

I managed to replicate your problem by having a 0.5 value of the Y of the parent object. Make sure your parent object is at 0,0,0

Upvotes: 0

Basile Perrenoud
Basile Perrenoud

Reputation: 4110

Since you are dividing by an int, I think that the result of transform.localScale.y / 2 might be 0 instead of 0.5 (although I don't undestand why your print would give the right value)

Upvotes: 2

Related Questions