Application.LoadLevel Obsolete? Nothing Suggested works
Its pretty much like the question states.
Scene Manager does not work.
This is from the tutorial by Pix Labs for making a health bar function as shown here at .https://www.youtube.com/watch?v=fkUpXpD5PRs.
It goes like this.
public class HealthControl : MonoBehaviour
{
public Image healthBar;
public float health;
public GameObject restartDialog;
void Start()
{
ShowRestartDialog(false);
}
void Update()
{
checkHealth();
}
void checkHealth()
{
healthBar.rectTransform.localScale = new Vector3(health / 100, healthBar.rectTransform.localScale.y, healthBar.rectTransform.localScale.z);
if (health <= 0.0f)
{
ShowRestartDialog(true);
}
}
void OnCollisionEnter(Collision other)
{
if (other.collider.CompareTag("Death"))
{
SubtractHealth(30.0f);
}
else if (other.collider.CompareTag("Life"))
{
AddHealth(30.0f);
}
}
public void SubtractHealth(float amount)
{
if (health - amount < 0.0f)
{
health = 0.0f;
}
else
{
health -= amount;
}
}
public void AddHealth(float amount)
{
if (health + amount > 100.0f)
{
health = 100.0f;
}
else
{
health += amount;
}
}
public void ShowRestartDialog(bool c)
{
if (c)
{
Time.timeScale = 0.0f;
}
else
{
Time.timeScale = 1.0f;
}
restartDialog.SetActive(c);
}
public void Restart()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Then Unity says this:
Assets/Scripts/HealthControl.cs(76,43): warning CS0618: `UnityEngine.Application.loadedLevel' is obsolete: `Use SceneManager to determine what scenes have been loaded'
So the revision attempted went like this
SceneManager.LoadScene(index);
It did not work.
Adding the other suggestion given from the editor as in adding the additional
using UnityEngine.SceneManagement;
Did nothing at all to make the code work.
The only modification I did was change the Tag to be found from LIFE to Life and ENEMY to Death and made sure the tags on the said objects had the same tag spellings. In any cause the last line of code with application manager isn't working at all and the "solutions" are non-existent.
Help would be appreciated and I can in turn if there is one send the notice to Pix Labs and others having similar issues with tutorials not functioning in the latest Unity released update.
I still have not tried 5.3, but I would try something like this:
Application.LoadLevel(Scene$$anonymous$$anager.GetActiveScene().buildIndex);
Answer by SVER · Dec 17, 2015 at 07:45 AM
I had this same issue, it was because I had a custom script already with the name SceneManager. If you do as well Unity will access this every time even if you've already imported SceneManagement...
To see if you can access it try accessing it using the full address in your code, it will call Unity's SceneManager regardless of whether or not you've imported it at the beginning of your script.
Just typing the line below won't be enough (at least it wasn't for me), even if you've imported unity's SceneManager correctly:
SceneManagement.SceneManager.LoadScene("scene_name");
Instead, for every reference try:
UnityEngine.SceneManagement.SceneManager
Here's an example of what ended up working for me:
public void RestartLevel()
{
Debug.Log("restart function called");
// Application.LoadLevel(Application.loadedLevel); //old function
UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex);
}
Obviously you don't want to type out the full address every time, so you'll probably want to create a shortcut or change YOUR SceneManager script name completely.
Hope this helps!
Yeah i found out the specific on this one at least.
You only do Scene$$anonymous$$anager.LoadScene("scene_name");
if you are doing a startup like from a main menu going to the first level.
Then when wanting to load what current level you are in such as restarting that scene or level you set it as;
Scene$$anonymous$$anager.LoadScene(PlayerPrefs.GetInt("currentscenesave"));
rather than : Application.LoadLevel(Application.loadedLevel);
or alternatively 'Scene$$anonymous$$anager.LoadScene (Scene$$anonymous$$anager.GetActiveScene().buildIndex); .
rather than :
Application.LoadLevel(Scene$$anonymous$$anager.GetActiveScene().buildIndex); which is an erroneous combination of code because its attempting to combine Application with Scene$$anonymous$$anager, blah, blah blah.
Answer by Jessespike · Dec 16, 2015 at 07:20 PM
using UnityEngine.SceneManagement;
Or you can call SceneManager like this:
SceneManagement.SceneManager.LoadScene("foo");
http://answers.unity3d.com/questions/1113197/applicationloadlevel-obsolete-but-scenemanagerload.html
Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene("foo");?
I thought that was "fool" LOL and i tried it anyways. but no luck yet and still tryin.
Answer by $$anonymous$$ · Dec 16, 2015 at 10:53 PM
So I tried this; and if i set the health down manually to zero even though everything is in place, the tags of the targets ans script on the player, and the UI plugged into the script, still no collision detection to cause a reaction. Sure it acts solid like its suppose to (the life and death objects), but no reaction from the UI.
This revision I made also now shows no errors but still isn't functioning.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class HealthControl : MonoBehaviour
{
public Image healthBar;
public float health;
public GameObject restartDialog;
void Start()
{
ShowRestartDialog(false);
}
void Update()
{
checkHealth();
}
void checkHealth()
{
healthBar.rectTransform.localScale = new Vector3(health / 100, healthBar.rectTransform.localScale.y, healthBar.rectTransform.localScale.z);
if (health <= 0.0f)
{
ShowRestartDialog(true);
}
}
void OnCollisionEnter(Collision other)
{
if (other.collider.CompareTag("Death"))
{
SubtractHealth(30.0f);
}
else if (other.collider.CompareTag("Life"))
{
AddHealth(30.0f);
}
}
public void SubtractHealth(float amount)
{
if (health - amount < 0.0f)
{
health = 0.0f;
}
else
{
health -= amount;
}
}
public void AddHealth(float amount)
{
if (health + amount > 100.0f)
{
health = 100.0f;
}
else
{
health += amount;
}
}
public void ShowRestartDialog(bool c)
{
if (c)
{
Time.timeScale = 0.0f;
}
else
{
Time.timeScale = 1.0f;
}
restartDialog.SetActive(c);
}
public void Restart()
{
SceneManager.LoadScene("loadScene");
}
}
Ready to snatch myself bald. lol.
P.S. I have no changed character collider to character controller for better physics, but it still is not detecting any collisions in the UI. Its detecting collision with normal movement just fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220612052840im_/https://answers.unity.com/themes/thub/images/avi.jpg)