'The object of type 'BallMovement' has been destroyed but you are still trying to access it
I checked all over SO but couldn't find an answer that worked (or at least that I understood how to implement).
I just started Unity not too long ago and I'm trying to make a Pong clone. The game work great on the first run, but when a player wins and it switches to scene 2, then you press the Play Again button and go back to scene 1 (gameplay scene) I get this error while trying to play:
heres my Start method for the problematic class.
public class BallMovement : MonoBehaviour
{
Rigidbody2D ball;
Vector2 vel;
// Start is called before the first frame update
void Start()
{
float posY = Random.Range(-150, 150);
ball = GameObject.FindGameObjectWithTag("Ball").GetComponent<Rigidbody2D>();
transform.localPosition = new Vector2(-19, posY);
vel.x = 250;
vel.y = -150;
ball.velocity = vel;
GameStateManager.PlayerScored += Start;
}
...
sorry for asking such a dumb/ vague question but I'm seriously stuck, I've been working on this pong clone for 8 hours and I managed to cross every hurdle on my own besides this
Solution 1:[1]
It seems you Delete/ Dispose the BallMovement class. I assume this is because you Delete/ Dispose the ball, and don't create the new ball with the BallMovement class/ script.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rbdeenk |

