Question by
McMattGames · Apr 15, 2016 at 05:43 PM ·
c#crashscene-loadingloadlevel
Application.LoadLevel crashes unity
I was playing with the roll a ball tutorial and was adding more things to it. I wanted to add multiple levels but when I run Application.LoadLevel it crashes. At first it told me to add the scenes to the build settings, so I did. After that it just crashes when I run the game. I tried switching to scenemanager but the import does not work.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float speed;
private int count;
private Rigidbody rb;
public Text countText;
public Text winText;
public Text loseText;
public Text hubText1;
public Image backImage;
public Button startButton;
public GameObject pickup1;
public GameObject pickup2;
public GameObject pickup3;
public GameObject pickup4;
public GameObject pickup5;
public GameObject pickup6;
public GameObject pickup7;
public GameObject pickup8;
public GameObject pickup9;
public GameObject pickup10;
public GameObject pickup11;
public GameObject pickup12;
public GameObject pickup13;
public GameObject pickup14;
public GameObject pickup15;
public GameObject pickup16;
private bool win;
private bool StartClicked;
void Start()
{
Application.LoadLevel("Level1");
//SceneManagement.SceneManager.LoadScene("Menu_Level"); gives error
hubText1.gameObject.SetActive(true);
startButton.gameObject.SetActive(true);
backImage.gameObject.SetActive(true);
loseText.gameObject.SetActive(false);
winText.gameObject.SetActive(false);
count = 0;
setCountText();
rb = GetComponent<Rigidbody>();
win = false;
count = 0;
}
public void startGame()
{
count = 0;
win = false;
StartClicked = true;
backImage.gameObject.SetActive(false);
startButton.gameObject.SetActive(false);
hubText1.gameObject.SetActive(false);
rb.gameObject.transform.position = new Vector3(0, 0, 0);
pickup1.gameObject.SetActive(true);
pickup2.gameObject.SetActive(true);
pickup3.gameObject.SetActive(true);
pickup4.gameObject.SetActive(true);
pickup5.gameObject.SetActive(true);
pickup6.gameObject.SetActive(true);
pickup7.gameObject.SetActive(true);
pickup8.gameObject.SetActive(true);
pickup9.gameObject.SetActive(true);
pickup10.gameObject.SetActive(true);
pickup11.gameObject.SetActive(true);
pickup12.gameObject.SetActive(true);
pickup13.gameObject.SetActive(true);
pickup14.gameObject.SetActive(true);
pickup15.gameObject.SetActive(true);
pickup16.gameObject.SetActive(true);
rb.isKinematic = false;
winText.gameObject.SetActive(false);
loseText.gameObject.SetActive(false);
countText.text = "Count: " + count;
}
void FixedUpdate()
{
if (win == false) {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(Input.acceleration.x/10, 0, Input.acceleration.z/10);
rb.AddForce(movement * speed);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
count += 1;
other.gameObject.SetActive(false);
setCountText();
}
else
{
if (other.gameObject.CompareTag("Danger"))
{
win = true;
rb.isKinematic = true;
rb.velocity = Vector3.zero;
loseText.gameObject.SetActive(true);
startButton.gameObject.SetActive(true);
backImage.gameObject.SetActive(true);
hubText1.gameObject.SetActive(true);
}
}
}
void setCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 16)
{
win = true;
rb.isKinematic = true;
rb.velocity = Vector3.zero;
winText.gameObject.SetActive(true);
startButton.gameObject.SetActive(true);
backImage.gameObject.SetActive(true);
hubText1.gameObject.SetActive(true);
// if (Application.loadedLevel.Equals("Level1")) {
//Application.LoadLevel("Level2");
// }else
// {
// if (Application.loadedLevel.Equals("Level2"))
// {
Application.LoadLevel("Minigame");
// }
// }
}
}
}
Comment
So I found that this only happens when Application.LoadLevel is used in the start function. Also, when I load a scene it is dark.
Your answer
Follow this Question
Related Questions
Save timer score between scenes? (C#) 5 Answers
How to fix Loading Screen? 0 Answers
How do i create a scene variable? 3 Answers
[SOLVED]My code line don't execute 1 Answer