- Home /
mystery RestartLevel function used in official tutorial
I am doing the official tutorial "Survival shooter". In chapter 6 "player health", we have the following snippet at the end of PlayerHealth.cs
:
public void RestartLevel()
{
Application.LoadLevel(Application.loadedLevel);
}
Adding this function really confuses me. It is simply not explained in the video, it's not in the official documentation, which I guess means this is a project specific function, but it's totally hidden where is this called. I did a string search in the project folder and the only place where it can possibly be called is Assets/Models/Characters/Player.fbx.meta
.
...
- serializedVersion: 16
name: Death
takeName: Take 001
firstFrame: 525
lastFrame: 650
wrapMode: 0
orientationOffsetY: 0
level: 0
cycleOffset: 0
loop: 0
loopTime: 0
loopBlend: 0
loopBlendOrientation: 0
loopBlendPositionY: 0
loopBlendPositionXZ: 0
keepOriginalOrientation: 0
keepOriginalPositionY: 1
keepOriginalPositionXZ: 0
heightFromFeet: 0
mirror: 0
bodyMask: 01000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000
curves: []
events:
- time: .997762918
functionName: RestartLevel
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
...
I tried to find this string everywhere in the inspector, but I couldn't find it at all. What is this mysterious hidden way of specifying a string in a model file which calls the function defined in the cs script? Why is this not documented anywhere at all in the tutorial?
How can I see / edit the property of the model file where this function is called from?
When I remove the function from the cs script, I get the following error in console:
'Player' AnimationEvent 'RestartLevel' has no receiver! Are you missing a component?
The method name can be whatever, RestartLevel is not something specific to unity as a method that is.
The point is, the animation event tries to invoke the RestartLevel method, the internal line in that method loads a level by the current level's index that you will see in the build settings.
I don't see that they added that method to the script in the officially listed script on 6 of 10, they do add it to a GameOver$$anonymous$$anager in the update method based off the player's health being less than or equal to 0.
I just watched the whole video, so, i'm not sure what you're looking at.
http://unity3d.com/learn/tutorials/projects/survival-shooter/player-health
Answer by HVent · Jan 07, 2015 at 12:48 AM
Hi. i can't speak english, but i try to type in english. using "google translate"... i think that the RestartLevel()function is called by animation event. if you go to project view - modles - characters and select player - animations tab in inspector, you can see to the "animation clip"( move, depth, idle) if you select to death clip and drag your scroll to bottom of the inspector, you can see the "Event" panel. there is downward-pointing arrow on right of Event panel. if you click the arrow, Editing Animation event dialog is opened and, RestartLevel() function is right there.
this is so hidden... but I remember hearing this in the tutorial video thanks!
You can see it on this part: https://unity3d.com/learn/tutorials/projects/survival-shooter/har$$anonymous$$g-enemies?playlist=17144 At 8:40