- Home /
My game changes to the second level, but then refreshes that level,My game changes to the second level, but then just reloads the second level
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody2D rb2d;
private int count;
private int LevelNo;
private int LevelChange;
void Start()
{
rb2d = GetComponent <Rigidbody2D> ();
count = 1;
winText.text = "";
SetCountText ();
LevelNo = 0;
LevelChange = 0;
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("PickUp"))
{
other.gameObject.SetActive (false);
count = count - 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Warp cores to stabilize: " + count.ToString ();
if (count == 0)
{
LevelChange = LevelNo + 1;
winText.text = "You saved the warp core!";
Application.LoadLevel ("Level " + LevelChange);
count = 1;
}
}
void FixedUpdate()
{
if (speed == 10) {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
rb2d.AddForce (movement * speed);
}
}
}
This script also controls scoring and movement.,using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody2D rb2d;
private int count;
private int LevelNo;
private int LevelChange;
void Start()
{
rb2d = GetComponent <Rigidbody2D> ();
count = 1;
winText.text = "";
SetCountText ();
LevelNo = 0;
LevelChange = 0;
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("PickUp"))
{
other.gameObject.SetActive (false);
count = count - 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Warp cores to stabilize: " + count.ToString ();
if (count == 0)
{
LevelChange = LevelNo + 1;
winText.text = "You saved the warp core!";
Application.LoadLevel ("Level " + LevelChange);
count = 1;
}
}
void FixedUpdate()
{
if (speed == 10) {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
rb2d.AddForce (movement * speed);
}
}
}
This script also controls the score and movement.
Answer by Captain_Pineapple · May 29, 2018 at 11:09 AM
Hey there,
for solving this kind of issue it might be helpfull for you to get a bit into Logging messages. For example to log what value you actually have in your "LevelChange" variable when you try to load the next scene. You will notice that it always will have the value 1.
This happens because your player Instance get's created new in each Loaded Scene. There you call the Startfunction and set the LevelNo and LevelChange variables to 0. Thus you can never escape loading the second Level again and again.
You should be able to fix your problem by changing the following things:
Remove the Line where you initially set LevelChange to 0 in the Start() function.
Change the LevelChange variable to
private static int LevelChange
actually increment the variables Value. So instead of
LevelChange = LevelNo +1;
have it sayLevelChange += 1;
This way your LevelCounter's value should be preserved even when loading a new scene. Hope this helps/works.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163617im_/https://answers.unity.com/themes/thub/images/avi.jpg)