- Home /
Restart the scene without changing the scoreboard
I was making a pong-like game and I have reached the point where I have to reset the ball to its original position every time a player scores in the opponent's goal. I have seen that you can use the SceneManager to reload the initial scene although this also resets the score to its original state.
I have also thought, instead of reloading the main scene, to simply reload a ball, using the Instantiate function. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MovPelota : MonoBehaviour
{
// Start is called before the first frame update
public float ballspeed,movx,movy;
float maximoX, maximoY;
float minimoX, minimoY;
float objectWidth, objectHeight;
public Vector2 screenBounds;
public Camera MainCamera;
public event System.Action PuntoEnemigo;
public event System.Action PuntoJugador;
public Vector3 originalpos = new Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z);//Original Position of the Ball
void Start()
{
screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, MainCamera.transform.position.z));
ballspeed = 5f;
objectWidth = transform.GetComponent<Renderer>().bounds.extents.x;
objectHeight = transform.GetComponent<Renderer>().bounds.extents.y;
//Maximo y minimo de las pantallas
maximoX = screenBounds.x * -1 + objectWidth;
minimoX = screenBounds.x - objectWidth;
maximoY = screenBounds.y * -1 + objectHeight;
minimoY = screenBounds.y - objectHeight;
//Movimiento de la pelota.
movx = -.5f;
movy = -.5f;
}
// Update is called once per frame
void Update()
{
Vector3 viewPos = transform.position;
Puntuaciones(viewPos);
if (viewPos.y <= maximoY)
{
viewPos.y = ((float)(maximoY - 0.05f));
movy *= -1;
}
if (viewPos.y >= minimoY)
{
viewPos.y = ((float)(minimoY + 0.05f));
movy *= -1;
}
transform.Translate(movx * Time.deltaTime * ballspeed, movy * Time.deltaTime * ballspeed, 0f);
}
void OnTriggerEnter2D(Collider2D trigger)
{
Vector3 viewPos = transform.position;
if (trigger.tag == "jugador")
{
viewPos.x = ((float)(maximoX - 0.05f));
movx *= -1;
}
}
void Puntuaciones(Vector3 PosicionNueva)//Function for points
{
if (PosicionNueva.x <= maximoX )
{
if(PuntoEnemigo != null)
{
PuntoEnemigo();
}
Instantiate(pelota,originalpos);//pelota is a game object in the scene
}
if (PosicionNueva.x >= minimoX)
{
if(PuntoJugador != null)
{
PuntoJugador();
}
Instantiate(pelota,originalpos);
}
}
}
I'm new to unity and c#, so I don't understand when it says that "ball" doesn't exist in this context, I don't know if it's because it has to be declared as a game object even though it exists in the main scene.
I wanted to know how to do what I am trying to do well or if there is another way to do it more efficiently. (17,78)Los errores que me aparecen son : Error CS0236: A field initializer cannot reference the non-static field, method, or property 'Component.gameObject' (17,110): error CS0236: A field initializer cannot reference the non-static field, method, or property 'Component.gameObject' (80,26): error CS0103: The name 'pelota' does not exist in the current context (90,25): error CS0103: The name 'pelota' does not exist in the current context