DragonCoder
DragonCoder

Reputation: 33

Changing a deprecated code piece in Unity (BoxCollider2D)

I am trying to use the box collider functionality in Unity, however it seems to be deprecated.

I get the message: "UnityEngine.BoxCollider2D.center" is obsolete. BoxCollider2D.center has been deprecated. Use BoxCollider2D.offset instead (UnityUpgradable)

I have been trying to set Walls at the edges of the screen. Here is the code :

//Move each wall to its edge location:

topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);

bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);

leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);

rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
//Move the players to a fixed distance from the edges of the screen:
Player01.position.x = mainCam.ScreenToWorldPoint (new Vector3 (75f, 0f, 0f)).x;
Player02.position.x = mainCam.ScreenToWorldPoint (new Vector3 (Screen.width -75f, 0f, 0f)).x;

TopWall, BottomWall, LeftWall and RightWall are of course all of type BoxCollider2D.

How should I change my code to not get this error message? Thank you for your help.

Upvotes: 0

Views: 2528

Answers (1)

Galman33
Galman33

Reputation: 154

"BoxCollider2D.center has been deprecated. Use BoxCollider2D.offset instead"

Replace:

topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);

With:

topWall.offset = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);

Of course do that change to BottomWall, LeftWall and RightWall too.

Upvotes: 1

Related Questions