Public void LoadLevel(string name)

Helloo everyone!
I cant understand how the( string name) works…
i know how the strings are working, but i cant understand the (name)

thank you.

2 Likes

“name” is the name of your scene. The scene that you want to load.

4 Likes

Hi Anthony, thank you for the reply! :grin:
But is it always like this?
I didn’t declare is as a variable or something…
Im sorry… Noob here, trying to have a solid foundation :stuck_out_tongue:

1 Like

By now, you’ve seen plenty of methods where you need parenthesis () to indicate a method call. For example, you might have something like Start() or Update().

Let’s say we make a method that picks up an item. Several things need to happen – an animation needs to play, a sound effect occurs, your inventory needs to be adjusted, and an item has to disappear off the ground. Because all this stuff needs to happen every time we pickup an item, it makes sense to have a method for this. That way we can just call the pickup item method every time we get near it, or when we open a chest, or at various other points in the game. Because so many things can cause the PickupItem() method to occur, we need to make it public.

We don’t want to make a separate method for every type of item we may pickup, “pickupApples”, “pickupBerries”, etc. So in addition to calling the method at a certain time, we also want to pass along some information related to the current situation. Let’s say, for purposes of this example, we want to pass along a string. We put this string in the parenthesis of the method call. For example, pickup(“Apple”) or pickup(“Berries”). It an actual game situation, this might be a reference to another object somewhere in the world such as pickup(collision.gameObject.name). In this case, you’re passing along the name of whatever object the player has run into. (When information is passed along in this way, it’s called an argument. I don’t know why.)

In order to pass this information to a method, the method has be setup for it. You do this by typing in a parameter in the parenthesis after you declare the method’s name. The parameter tells the method what kind of information to accept, and puts that information in a local variable for the method to use.

So let’s say we want to make a method called Pickup(). It’s going to be:

public void Pickup ()
{
//some stuff here
}

But we also want it pass along a string with the method call, such as Pickup(“Apple”) so we need to tell it to receive strings. We do this by declaring a variable right in the method declaration. For example, “string item”. That’s all it takes to declare something, you give a type and a name. When you declare something at the top of your class, it becomes available for all the methods in that whole class. When you declare something in a method, it only exists to be used by that one method.

So we have:

public void Pickup (string item)
{
//some stuff here
}

The name of this variable will be item. The type of variable is string. The reason we have to put “string” there is because the first time we ever use or declare a variable, we have to specify the type of information it will hold. It may hold an integer, a string, a Vector3, a GameObject…anything.

So when this method is called, all the information that is passed to it gets put into the variable called item, which is created (declared) just for this purpose. (If the information being received isn’t a string, you’re going to get an error, because you told it to accept a string.)

You couldn’t just type: public void Pickup (string) because you haven’t declared the variable as part of your parameter. (Actually, that might work. I’m not sure. But it wouldn’t do any good. The string would get passed to the method, but your method doesn’t store the information anywhere so it would be lost.)

Hopefully that helps clear some stuff up for you. If not, go ahead and ask any questions you have and I’ll try to make my answers shorter and clearer. The structure of variables is very important.

This may also help: https://unity3d.com/learn/tutorials/topics/scripting/variables-and-functions

4 Likes

Anthony, I just love you bro!
I got it, and I am really thankful for your help.
Didn’t know that i can get that much of a help here…

1 Like

Unfortunately I am still extremely confused.

So the information we feed the string with is the Name of the level I typed in in the “On Click()” field in the Button (script)?

@super_taskdone and anthony (I won’t attach him to not bother him) thank you both. If one of you did not asked other won’t answer then we will have to search for this information. Thanks both of you guys

1 Like

That’s pretty much correct. If I’m understanding the context right:

You have a UI button. In the OnClick() information, you are selecting a SceneLoader instance, running a LoadScene method, and passing along a string.

The reason you need to pass a string is because the LoadScene method has a string parameter, so it’s signature looks like this:

void LoadScene (string name)

When you click on the UI button, it calls that LoadScene method. When it does so, it also sends that string text in it’s OnClick() info as an argument. When that argument is passed into the method’s parameter, a new variable of type string is created which has been initialized with the string passed from the button. This local variable is called “name” (because that’s the name I specified in the parameter).

Privacy & Terms