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 Calum1015 · Jun 08, 2015 at 03:01 AM · arrayperformanceif-statementsintinteger

Checking to see if a loaded level is equal to a member of an integer array

I am currently creating a mouse lock/hide script and have an object in each scene with a script. The script checks a list of if statements to find which level is being used by using Application.loadedLevel. Currently with this setup I will need TONS of if statements. So I was wondering if I could create a whitelist/blacklist sort of setup to check an array of integers and find if any integers in that array was the currently loaded level. Could this be done? Any help would be great since I feel that all those if statements will drop performance by a lot. Thanks!

Comment
Add comment · Show 3
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 Priyanshu · Jun 08, 2015 at 06:16 AM 0
Share

in a for loop check if whitelist[i] == Application.loadedLevel.

avatar image Calum1015 · Jun 10, 2015 at 11:10 PM 0
Share

$$anonymous$$y code is this: using UnityEngine; using System.Collections;

 public class cursorLockedFalse : $$anonymous$$onoBehaviour {
 
     public int [] whitelist;
     public int [] blacklist;
     
     void Awake () {
         for(int i=0; i<100; i++)
         {
             if(whitelist[i] == Application.loadedLevel)
             {
                 Cursor.visible = false;
                 Screen.lockCursor = true;
             }
             else if(blacklist[i] == Application.loadedLevel)
             {
                 Cursor.visible = true;
                 Screen.lockCursor = false;
             }
         }
     }
 }

It's still not working correctly. It doesn't lock the cursor or make the cursor invisible on the whitelist. Any help would be appreciated.

avatar image Priyanshu · Jun 11, 2015 at 01:36 PM 0
Share

This code will iterate through both the Lists.

If loaded level matches to level of either List. It will do the assigned work.

 foreach( int level in whitelist)
 {
    if( level == Application.loadedlevel)
    {
         Cursor.visible = false;
         Screen.lockCursor = true;
    }
 }
 
 foreach( int level in blacklist)
 {
    if( level == Application.loadedlevel)
    {
         Cursor.visible = false;
         Screen.lockCursor = true;
    }
 }

1 Reply

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

Answer by crohr · Jun 10, 2015 at 11:30 PM

Your code causes an IndexOutOfBounds exception if either list is below 100 in length. Also, I assume that you want this to check every time your game loads a new level.

If you attach the following script to an empty gameObject in the first scene that is loaded, you should be golden. (You may notice that at first the cursor doesn't actually disappear, just click in the game window to focus it and the cursor go away.)

 using UnityEngine;
 using System.Collections;
 
 public class cursorLockedFalse : MonoBehaviour {
     
     public int [] whitelist;
     public int [] blacklist;
 
     private void Awake()
     {
         DontDestroyOnLoad(gameObject);
     }
     
     private void OnLevelWasLoaded (int level)
     {
         for(int i=0; i<whitelist.Length; i++)
         {
             if(whitelist[i] == Application.loadedLevel)
             {
                 Cursor.visible = false;
                 Screen.lockCursor = true;
             }
         }
 
         for(int i=0; i<blacklist.Length; i++)
         {
             if(blacklist[i] == Application.loadedLevel)
             {
                 Cursor.visible = true;
                 Screen.lockCursor = false;
             }
         }
     }
 }
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 Calum1015 · Jun 12, 2015 at 12:23 AM 0
Share

Works like a charm! Thanks for your help!

avatar image crohr · Jun 12, 2015 at 01:20 AM 0
Share

You're welcome!

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

22 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

Related Questions

Error with an Array 1 Answer

How to convert String array to int array 1 Answer

Number parse 1 Answer

Check if an int is equal to an array int 3 Answers

How do I create a save scene feature though int value in c# 2 Answers


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