Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by mcmalonzo · Sep 25, 2017 at 04:24 PM · error message

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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 
 
  }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mcmalonzo · Sep 25, 2017 at 05:52 PM 0
Share

thanks a lot ^_^

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with Error Code CS0019 2 Answers

Assertion failed: Invalid worldAABB. Object is too large or too far away from the origin. 2 Answers

Failed extracting collision mesh error 0 Answers

unity 5.6 webgl error when run project,error CS0117: `UnityEngine.Application' does not contain a definition for `WebGLPlayer' 0 Answers

Scripting problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges