One of the uses of methods is changing the properties of an object live as your game runs. If you are a beginner and want to learn how to build virtual reality games, check out our Unity 3D course!
Suppose you want to change the size of the cube while the game is running. You can use the Update
method to achieve this. Cut the line in the Start
method. Paste it into the Update
method in place of the Log
line. Edit the line so that it appears as so:
// Use this for initialization
void Start () {
}
// Called once per frame.
void Update () {
transform.localScale = Vector3.one * sizeModifier;
}
In the preceding code, we set Cube’s local Scale to be the original vector (1 1 1) multiplied by sizeModifier
. Save the script, and open Unity. Press Play. Cube will have the dimensions 0.1 0.1 0.1. Click on the “Size Modifier” label (text) in the Cube (Script) property, and drag your mouse left and right. Cube’s size will change according to the value of Size Modifier.
Stop playing the scene. Set Size Modifier to 2.5. Next we will change the name of the cube. Currently, you can see in the Hierarchy that the name of the cube is “Cube”. In Cube.cs, type the following line into the Start
method.
// Use this for initialization
void Start () {
transform.name = newName;
}
Save the script, and open Unity. Press Play. “Cube” in the Hierarchy will be renamed “Cubey42”.
What if we wanted to define a new method other than Start
and Update
? Suppose we want to use a method to add some characters to Cubey42
‘s name. Below the Start
method, type the following code to declare the method ImproveName
.
// Use this for initialization
void Start () {
transform.name = newName;
}
string ImproveName (string originalString) {
}
string ImproveName
means the method returns a string. The method has the string originalString
as a parameter.
Save the script, and open Unity. The Console will show the following error message.
The message states that the file Cube.cs in Assets, contains an error at Line 20 in Row 16. The error is that not all code paths return a value in ImproveName
. This error occurs because ImproveName
needs to return a value. Open Cube.cs. Use the following code to return 10
.
string ImproveName (string originalString) {
return 10;
}
Save the script, and open Unity. An error will appear in the Console saying that an integer cannot be converted to a string.
ImproveName
returns a string, but 10
is an integer. Use the following code to return originalString
, the method’s parameter. + appends “-[” and “]-” around originalString
.
string ImproveName (string originalString) {
return "-[" + originalString + "]-";
}
Call the ImproveName
method in the Start
method. The following code makes newName
a parameter of ImproveName
. ImproveName
will edit newName
to contain the string that ImproveName
returns.
void Start () {
transform.name = ImproveName(newName);
}
Save the script, and open Unity. Press Play. “Cubey42” in the Hierarchy will become “-[Cubey42]-“. Want to learn more about methods? Check out our Unity 3D course, where you build 30 virtual reality games!