c# Unreachable code detected
hi to all! I got this error (CS0162 C# Unreachable code detected) but I don't get why... this is my script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GamecontrollerBeha : MonoBehaviour { public bool invent, invsaver, oggetto1, oggetto2, oggetto3, ogg1sel, ogg2sel, ogg3sel, mangiato, lavato, vestito; public GameObject inv, ogg1, ogg2, ogg3; private GameObject canvfind; public static GamecontrollerBeha instance; public Transform invspawn; public string savefile;
public string SaveToString()
{
return savefile = JsonUtility.ToJson(this);
PlayerPrefs.GetString(savefile); /**** <- ERROR happening HERE ****/
}
void Awake()
{
if (instance == null)
{
DontDestroyOnLoad(gameObject);
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
}
public void Start()
{
gameObject.tag = "gamecontroller";
}
public void Update()
{
canvfind = GameObject.FindGameObjectWithTag("canvas");
if (Input.GetKeyDown("space"))
{
Vector3 spawncorrector = new Vector3(invspawn.position.x, invspawn.position.y - 1, 0);
Vector3 spawncorrector1 = new Vector3(invspawn.position.x - 2.3F, invspawn.position.y - 0.5F, -1);
Vector3 spawncorrector2 = new Vector3(invspawn.position.x - 2.3F, invspawn.position.y - 1.5F, -1);
Vector3 spawncorrector3 = new Vector3(invspawn.position.x - 2.3F, invspawn.position.y - 0.5F, -1);
if (invsaver == false)
{
if (invent == true)
{
invent = false;
Destroy(GameObject.FindWithTag("inventario"));
Destroy(GameObject.FindWithTag("oggetto1"));
Destroy(GameObject.FindWithTag("oggetto2"));
Destroy(GameObject.FindWithTag("colazione"));
}
else if (invent == false)
{
GameObject inve = Instantiate(inv, spawncorrector, invspawn.rotation);
inve.transform.SetParent(canvfind.transform);
invent = true;
if (oggetto1 == true)
{
GameObject og1 = Instantiate(ogg1, spawncorrector1, invspawn.rotation);
og1.transform.SetParent(canvfind.transform);
}
if (oggetto2 == true)
{
GameObject og2 = Instantiate(ogg2, spawncorrector2, invspawn.rotation);
og2.transform.SetParent(canvfind.transform);
}
if (oggetto3 == true)
{
GameObject og3 = Instantiate(ogg3, spawncorrector3, invspawn.rotation);
og3.transform.SetParent(canvfind.transform);
}
}
}
}
}
} thanks from now for the help!
Answer by Cuttlas-U · Sep 25, 2017 at 05:46 PM
hi; u cant write any lane of code after u use "return" ;
cause the function will be over in that lane and it will not continue to the next lane ; so this lane of code should always be your last lane ;
public string SaveToString()
{
PlayerPrefs.GetString(savefile);
return savefile = JsonUtility.ToJson(this); // this should be last lane
}
Answer by nir_unity1 · Jul 06, 2021 at 10:29 AM
In general returns stop the function right there and return the value. it will not continue any further part of the answer afterwards, chance making the code unreachable.
but there is 1 exception where the unreachable warning can be false positive. while writing { if(condition A) return;
//assuming condition A is not met
do X...
}
is a common practice, when you use it in Switch statement
Switch (val)
{
case 1:
return 1;
break;
case 2:
return 2;
break;
case 3:
return 3;
break;
}
you may still receive this warning as unity might not detect the option that the condition is not met before runtime. (happens on my unity 20.1.11). it should be considered a bug, but it still happens.
in your example, however, you did the classic mistake of adding more lines of code that will never be executed.