Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Statement · Mar 30, 2011 at 01:05 AM · switchreturnjavascript-specific

JavaScript switch returns warn for unreachable code.

I am getting a weird warning in a switch statement that return back values based on the type of parameter, but it makes no sense to me why this happen! Is it because it expect fall-through to the other cases? When I double click the error it highlights the following line of code: switch (parameterType) {.

Obviously I can't put break in there either since it would warn for the break being unreachable. Is the only solution to set a return variable and break out of it or can I squelch the warning otherwise?

In C# it's possible to ignore selective warnings:

#pragma warning disable 414, 3021

BCW0015: WARNING: Unreachable code detected.

function GetParam(gameObject : GameObject, interactor : GameObject) {
    switch (parameterType) {
    case ParameterType.NoParameter:
        return null;
    case ParameterType.GameObject:
        return gameObject;
    case ParameterType.Interactor:
        return interactor;
    case ParameterType.Str:
        return parameter;
    case ParameterType.Float:
        return float.Parse(parameter);
    case ParameterType.Integer:
        return int.Parse(parameter);
    case ParameterType.FindChild:
        return gameObject.transform.Find(parameter);
    case ParameterType.FindGameObject:
        return GameObject.Find(parameter);
    case ParameterType.GetComponent:
        return gameObject.GetComponent(parameter);
    }
}

No warnings :P

function GetParam(gameObject : GameObject, interactor : GameObject) {
    var result;
    switch (parameterType) {
    case ParameterType.NoParameter:
        result = null;
        break;
    case ParameterType.GameObject:
        result = gameObject;
        break;
    case ParameterType.Interactor:
        result = interactor;
        break;
    case ParameterType.Str:
        result = parameter;
        break;
    case ParameterType.Float:
        result = float.Parse(parameter);
        break;
    case ParameterType.Integer:
        result = int.Parse(parameter);
        break;
    case ParameterType.FindChild:
        result = gameObject.transform.Find(parameter);
        break;
    case ParameterType.FindGameObject:
        result = GameObject.Find(parameter);
        break;
    case ParameterType.GetComponent:
        result = gameObject.GetComponent(parameter);
        break;
    }
    return result;
}
Comment
Add comment · Show 2
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 Jesse Anders · Mar 30, 2011 at 02:28 AM 0
Share

I have a vague recollection of this topic (or a similar one) being discussed recently, but I don't know where or when that was. What if you add 'default: return null;' to the first example; does that get rid of the warning?

avatar image Statement · Mar 30, 2011 at 11:47 AM 0
Share

No, default doesn't help. In C# you can ignore warnings selectively. I was hoping to be able to do the same in JS, but the syntax won't work in JS (see updated question).

1 Reply

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

Answer by Mike 3 · Mar 30, 2011 at 11:53 AM

I don't believe UnityScript has the ability to do it - the preprocessor directives it exposes are pretty lacking

Comment
Add comment · Show 2 · 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 Statement · Mar 30, 2011 at 12:02 PM 0
Share

I didn't start out with JS as I learned Unity so I don't know where to find the proper resources, but maybe you do? Is there any place where I can get a specification of the JS implementation? I'm sick of guessing and I thought I'd learn it once and for all.

avatar image Mike 3 · Mar 30, 2011 at 12:47 PM 0
Share

There isn't one - the stuff I know is a mix of the data on the wiki, the documentation, and a few years of helping in IRC :S

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

No one has followed this question yet.

Related Questions

Return Switch combined statment causing errors 1 Answer

Switch cases malfunction? 0 Answers

How to return from the last case to the first 1 Answer

Display the speed of a Game Object in MPH 1 Answer

2Dimensional Array? 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