Here I’m developing a basic arkanoid game where ball bounces off a paddle and gamestatus.cs script keeps track of all score and powerups so i kept it as a dontdestroyonload but because of this as the scene changes the paddle references are missing and so does the balls so i made the paddle as dontdestory but kept the ball as it is. On the ball script there are two functions LockonPaddle and OnLaunchClick.
public void LockOnPaddle()
{
Vector2 PaddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = PaddlePos + MouseToPaddlePos;
}
public void LaunchOnClick()
{
if(Input.GetMouseButtonDown(0))
{
HasStarted = true;
myRigidBody2D.velocity = new Vector2(LaunchX,LaunchY);
}
}
But when the scene changes the ball references are missing as it gets destroyed in the previous scene.
and if i kept the ball as a dontdestroy it doesn’t gets lockonpaddle on the next scene, it starts from its last position on previous scene.
So i need some help regarding when the scene changes here
public void DestroyBlocks()
{
BreakableBlocks--;
if(BreakableBlocks <= 0)
{
sceneLoader.LoadNextScene();
FindObjectOfType<Ball>().GetComponent<Rigidbody2D>().velocity = Vector2.zero;
FindObjectOfType<Ball>().LockOnPaddle();
FindObjectOfType<Ball>().LaunchOnClick();
}
}
i want to make the ball again stick to the paddle and launch on click .
and the below is the lose on collider script where the ball collides the object and new ball is instantiated.
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("NormalBall") == true)
{
if(lifePower == 0)
{
collision.GetComponent<Collider2D>().isTrigger = true;
gameStatus = FindObjectOfType<GameStatus>();
gameStatus.ChangePosition();
DestroyLevel();
}
else
{
for (var i= lifePower-1; i < lifePower; i++)
{
Instantiate(ball,new Vector3(paddle.transform.position.x, 0.799f, 0), Quaternion.identity);// Here Instead of Instantiating is it possible to transform the ball position and get it back on paddle.
}
GameStatus.countLives--;
}
}
else
{
collision.GetComponent<Collider2D>().isTrigger = false;
}
}
A little help would be much appreciated.