Background Sprites lose their tags when the project is built
I am currently building my first 2d game in unity and playing around with a 2D Box Collider/Rigidbody to move background tiles forward to make the background "infinite." This works just fine in the editor when I hit "play," but when I build it for both android and PC the background tiles stop being moved.
Here is my function for collision detecting:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Background")
{
Vector3 temp = collision.transform.position;
float width = ((BoxCollider2D)collision).size.x;
temp.x = lastBGX + width;
lastBGX = temp.x;
collision.transform.position = temp;
}
}
Where lastBGX is the x value of the edge of the last background tile
After some debugging it seems that the issue may be in the awake() function for my background collector:
void Awake ()
{
backgrounds = GameObject.FindGameObjectsWithTag("Background");
lastBGX = backgrounds[0].transform.position.x;
for (int i = 1; i < backgrounds.Length; i++)
{
if (lastBGX < backgrounds[i].transform.position.x)
{
lastBGX = backgrounds[i].transform.position.x;
}
}
}
When I run the code in the unity editor the FindGameObjectWithTag() function works just fine, but when I build it it stops working. Somehow it seems as if my background tiles are losing their tags in the process of the build.
Your answer
Follow this Question
Related Questions
Collision Triggers at Random 1 Answer
2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer
Collider2D not stopping when colliding with another Collider2D [Solved] 2 Answers
rb.velocity.y sometimes requires stopping on x axis as well. 0 Answers
Circle Collider 2d is slipping off box colider2d when it shouldnt 1 Answer