Multiplayer Netcode Bug

Basically, when the ball is gray and collides with the ground, it changes a variable in the ground which causes the ground sprite to change, but the variable only changes after reseting play mode.

Why would this be happening? (I couldn’t explain it without the gif) (pls bruh i’m strugglin here)

.

Untitledvideo-MadewithClipchamp-ezgif.com-video-to-gif-converter (1)

Post your code. We don’t even know what that “variable” is and how you’re changing it nor how you change the sprite color.

On the ball’s script it’s this, but the variable aint changing

void OnCollisionEnter2D(Collision2D col)
{
        if (newSprite == ballSprites[1]) //when it's gray
        {
            if (col.gameObject.name == "Ground 1-1")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 15 || GetComponent<Rigidbody2D>().velocity.y < -15)
                {
                    ground11.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 1-2")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground12.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 1-3")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground13.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 1-4")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground14.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 1-5")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground15.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 2-1")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground21.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 2-2")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground22.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 2-3")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground23.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 2-4")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground24.Damage = 5;
                }
            }

            if (col.gameObject.name == "Ground 2-5")
            {
                if (GetComponent<Rigidbody2D>().velocity.y > 13 || GetComponent<Rigidbody2D>().velocity.y < -13)
                {
                    ground25.Damage = 5;
                }
            }
        }
}

What’s the definition for groundXX variables? If these aren’t or don’t contain NetworkVariable instances and you don’t send them via RPC calls they will not be shared across the network.

Also in general, it’s bad practice to compare GameObject names or find them by name. You never know what will break if you rename them, and instantiating objects will add a “(Clone)” suffix to the name, and many more reasons.

And you should not repeat calls like GetComponent every time, it makes the code much harder to read and maintain, and also a bit slower with every extraneous call.

Use a local variable:

            var collidedObjectName = col.gameObject.name;
            var rigidbody = GetComponent<Rigidbody2D>();
            var velocity = rigidbody.velocity;
            if (collidedObjectName == "Ground 1-1")
            {
                if (velocity.y > 15 || velocity.y < -15)
                {
                    ground11.Damage = 5;
                }
            }
            // and so forth ...
1 Like