- Home /
need an updated command for application.loadlevel for unity 5.5 version
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManager : MonoBehaviour {
public static int currentScore;
public static int highScore;
public static int currentLevel= 0;
public static int unlockedLevel;
public static void CompleteLevel()
{
if (currentLevel < 2) {
currentLevel++;
Application.LoadLevel(currentLevel);
} else {
print ("you win");
}
}
}
Answer by Arylos · Aug 13, 2017 at 06:18 AM
Application.LoadLevel() has been deprecated since (I think) 5.2. You use SceneManager now so change your code to the following.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public static int currentScore;
public static int highScore;
public static int currentLevel= 0;
public static int unlockedLevel;
public static void CompleteLevel()
{
if (currentLevel < 2) {
currentLevel++;
SceneManager.LoadScene(currentLevel);
}
else
{
print ("you win");
}
}
Refer to the SceneManager documentation: C# Scripting API: SceneManager
Also, I forgot a bracket in my code so don't forget to add a final bracket at the end!
The most preferred way to reload the current level is to actually remove the variable currentLevel and replace the function with SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);, although that's a preference by me so that way I don't have an extra variable and the code automatically works on any level application.
Also, I missed a bracket in my code so don't forget to add that!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public float moveSpeed;
private float maxSpeed=5f;
private Vector3 input;
private Vector3 spawn;
public GameObject deathParticles;
// Use this for initialization
void Start () {
spawn = transform.position;
}
// Update is called once per frame
void Update () {
input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
if (GetComponent<Rigidbody> ().velocity.magnitude < maxSpeed) {
GetComponent<Rigidbody> ().AddForce (input * moveSpeed);
}
if (transform.position.y < 0.5) {
die();
}
}
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "enemy") {
die();
}
}
void onTriggerEnter(Collider other)
{
if (other.transform.tag == "goal") {
Game$$anonymous$$anager.CompleteLevel();
}
}
void die()
{
Instantiate (deathParticles, transform.position, Quaternion.identity);
transform.position = spawn;
}
}
can u relate this code with the above one... my player when hits the goal,it does nothing.the isTrigger is checked. can u plz send the correct code for my game to go to the next level
If it's a 2D game, then it's not the issue with your code, which is what I see so far. You may need to change how your physics interact with each other. This is a complex issue based on how your physics is setup.
You might need to refer to the Box Collider documentation as it has a helpful chart that's useful in seeing if messages are being sent or not at the bottom. So, if your player has a static trigger collider, you will need to add a rigidbody to your enemies, and vice versa. Playing with those cause different effects (rigidbodies obey gravity and will start "falling" while kinematic rigidbodies don't obey gravity, but only move when you specify them too. $$anonymous$$inematics are helpful for player controllers).
Overall, it just depends on what colliders you have set up. I recommend referring to that chart to see what you need to do to the individual elements.
Answer by tanoshimi · Aug 13, 2017 at 06:28 AM
Surprised that you didn't get a message about this, but you need to use the SceneManager class instead:
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
Your answer
Follow this Question
Related Questions
Application.LoadLevel not working on iOS 2 Answers
How can i hide a gameobject ? 1 Answer
How to load Level using scene object?? 2 Answers
Don't lose variable data when reloading a level - c# 3 Answers