Buttons Simulator 2016

As you may or may not know from my WIXOSS blog entry, currently the only thing working about that game is the menu (and even then really just one button does anything, although it does play music!).  However, I recently discovered that while my ‘settings’ button was taking me to the ‘settings scene’.  It was more complicated than it should have been.  Thanks to “Learn How to Code By Making Games – The Complete Unity Developer” course on Udemy.  I was able to learn a much easier process of doing it.  In other words, Button Simulator got a major update!!!!!

In Unity each level is called a scene.  So if I want to go from my start menu scene to my setting menu scene I have to tell the button which scene or ‘level’ I want it to go to.

Originally I wrote a script that handled the transitions, it looked something like this.

public void start_menu(){

Application.LoadLevel(1);

}

public void settings_menu(){

Application.LoadLevel(4);

}

So in that example using start_menu() will load the scene labeled as 1 in the build, which is the start scene.  Now there are a lot of things wrong with this.  For one, I would have to write a line of code for each single scene, as you see with my settings_menu().  Then I have to keep track of which number it is in the build.  What if I change a number of something?  Then everything breaks. Bad!  Bad programming!

So how did I learn to do it the right way (or atleast in a better way)?  Well, first off I created a new game object called LevelManager.  Then I gave that object my script as a component.  Then I got rid of all the previous code I had written and wrote this one line.

public void loadLevel(string name){

Application.LoadLevel(name);

}

That one line does the work of ALL the other lines I had and would have had in the future for changing levels.  It’s so simple!  The LoadLevel() function apparently looks in the build for either the number or the name.  So when you connect your button to the LevelManager object we created and specify loadLevel(string name) as the function to run onClick() it will give us a place to input a string.  That is where we put the name of the scene we want to go to!

loadlevel

So on the settings button on the start menu we put settings_menu as the name and ta-da! It goes to that scene.  This is great because if we know what button we are working on, it is easy to keep track of which scene we want to go to. If we need to change the build order, it will just search for the names, so it doesn’t matter.

So WIXOSS got an update today.  Buttons should now respond better, or at the very least, more efficiently.  (Really it’s just good design, probably didn’t have an effect on game speed, but I like to think it’s a big change!)  Just a really good trick, I am glad I learned!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s