top of page

011-Exercise-4 Setting States

1.1 Introduction

1. In this exercise we have a script called as MyPlayer011 which is present in folder 011.

2. For this script we are having three fields-age, playerName and playerHealth.

3. Design Editor script called MyPlayer011Editor for MyPlayer011 script having 3 buttons.Each of the button sets the MyPlayer011 into different states.

a. StateA-

age=0

playerName="A"

playerHealth=0

b. StateB-

age=20

playerName="B"

playerHealth=50

c. StateC-

age=100

playerName="C"

playerHealth=100

​

​

1.2 Basic Script

1. Let us open MyPlayer011 script and take a look at its content.

image.png
1.3 Solution

1. First let us make a custom editor for MyPlayer011 and call it as MyPlayer011Editor.

2. Make sure that this inherits from Editor and has CustomEditor attribute on top of it pointing to MyPlayer011 script.

3. Let us override the OnInspectorGUI() method and also add base.OnInspectorGUI() to draw the default inspector.

image.png
1.4 Adding buttons

1. Let us add 3 buttons to the custom Editor and call them as "StateA","StateB" and "StateC"

​

image.png
image.png
1.5 Adding methods for different states

1. Let us now add three methods and each of the method is going to be setting the MyPlayer011 to a particular state.Call these methods as StateA,StateB and StateC.

2. In each of these methods we are going to get a reference to target that is going to be casted as MyPlayer011.

3. After this we change the values of the variables.

image.png
image.png
1.6 Testing

1. Let us now add three methods and each of the method is going to be setting the MyPlayer011 to a particular state.Call these methods as StateA,StateB and StateC.

2. In each of these methods we are going to get a reference to target that is going to be casted as MyPlayer011.

3. After this we change the values of the variables.

image.png
image.png
image.png

Pressing State A Button

Pressing State B Button

Pressing State C Button

End

Kindly support me through my Patreon account if you find these tutorials useful. Feel free to connect with me on Twitter. This support enables me to create more educational content.

bottom of page