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
0
Question by InsaneGoblin · Jul 13, 2014 at 03:23 PM · c#arrayswitch

C# Array Issue

Pretty simple code, really don't understand why I can't reassign arrays in these circumstances (error CS0165: Use of unassigned local variable `trapToChoose'):

     int[] trapToChoose;

     switch (gameManagerScript.landscape)
     {
     case "City":
         if (floorNumber < 51)
         {
             trapToChoose = new int[] {20, 50, 70, 100, 100, 100};
         }
         else if (floorNumber > 50 && floorNumber < 101)
         {
             trapToChoose = new int[] {20, 50, 70, 100, 100, 100};
         }
         else if (floorNumber > 100)
         {    
             trapToChoose = new int[] {20, 50, 70, 100, 100, 100};
         }
         break;
     case "Forest":
         if (floorNumber < 51)
         {
             trapToChoose = new int[] {20, 50, 70, 100, 100, 100};
         }
         else if (floorNumber > 50 && floorNumber < 101)
         {
             trapToChoose = new int[] {20, 50, 70, 100, 100, 100};
         }
         else if (floorNumber > 100)
         {
             trapToChoose = new int[] {20, 50, 70, 100, 100, 100};
         }
         break;
     default:
         print (" PopTrap Script Error");
         trapToChoose = new int[] {0, 0, 0, 0, 0, 0};
         break;
     }

     randomPicker = (int) Mathf.Round(Random.Range(0, 100));
     
     if (randomPicker <= trapToChoose[0])
         chosenTrap = 0;
     else if (randomPicker > trapToChoose[0] && randomPicker <= trapToChoose[1])
         chosenTrap = 1;


Also, would love to know why I am forced to use the {} for the If statement inside a switch (embedded statement error)

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
1
Best Answer

Answer by robertbu · Jul 13, 2014 at 03:25 PM

The problem is that the compiler sees a way through your code that has you using 'trapToChoose' without initializing it. In this case, the problem happens if 'landscape' is something other than 'City'. 'trapToChoose' is not initialized, and therefore your use of it on line 14 is an error.

Note I did not get an error when I removed the {} from the 'if' statement.

Comment
Add comment · Show 11 · 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 InsaneGoblin · Jul 13, 2014 at 03:27 PM 0
Share

That is just a part of the code. There are 6 cases, finalizing with the "default" one.

avatar image robertbu · Jul 13, 2014 at 03:28 PM 0
Share

Okay, but what happens if the switch is "City" and the floor is > 51?

avatar image Tehnique · Jul 13, 2014 at 03:29 PM 1
Share

Just initialize your array with a "new int[size]".

avatar image Lo0NuhtiK · Jul 13, 2014 at 03:31 PM 0
Share

...and as far as forced braces go, I don't recall ever having to use them for a single-line if-statement; maybe that's just happening because of the array error, not sure.

avatar image InsaneGoblin · Jul 13, 2014 at 03:36 PM 0
Share

Put all the code of that method, so there are no misconceptions. Hopefully you can see what I can't

Show more comments
avatar image
0

Answer by NggingCrew · Jul 13, 2014 at 04:41 PM

 int[] trapToChoose = new int[6];
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# SetActive GameObject Array 2 Answers

Unity 3 GD HotShot-Tutorial Problem with an C#-Array 1 Answer

Saving 30 bools in an array possible? 2 Answers

Switching Weapons (C#) 2 Answers

Array iteration 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