Skip to main content

037. Unity3d Tutorial - Instantiating Our Character 5/5

See video

We continue with another tutorial in our Hack And Slash demo made with the Unity3d Game Engine.

In this video, We will continue creating a new scene and instantiating our new character in it with its new graphics and player character script attached.

Unity API: 
Tags:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Offline
Joined: May 23 2011

Somehow it don't work for me. My vitals still stay at default value(1). In My script I got exactly same code what is in video. Any Ideas ?

Offline
Joined: Nov 16 2011

try going through the character generation screen again then try it worked for me when i had that problem

Offline
Joined: Dec 23 2011

I most certainly have the exact same code as Petey and up until this point right here, I haven't had any compiling errors or wrong outputs aside from the simple mistakes Petey was also making in the videos. My return values of the vitals are also setting to 1 and I've gone thru the CharacterGeneration scene to assign the values. It still outputs at 1 and I'm having trouble figuring out why.

just for show of good faith I'll paste the LoadCharacter(); function

public void LoadCharacterData() {
GameObject pc = GameObject.Find("pc");

PlayerCharacter pcClass = pc.GetComponent();

pcClass.Name = PlayerPrefs.GetString("Player Name", "Name Me");

for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++ ) {
pcClass.GetPrimaryAttribute(cnt).BaseValue = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Base Value", 0);
pcClass.GetPrimaryAttribute(cnt).ExpToLevel = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Exp To Level", 0);
}

for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++ ) {
pcClass.GetVital(cnt).BaseValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Base Value", 0);
pcClass.GetVital(cnt).ExpToLevel = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Exp To Level", 0);

//Call this to update the adjustedbasevalue will be updated before you try and call the curvalue
pcClass.GetVital(cnt).Update();

//Get stored value for the CurValue for each Vital
pcClass.GetVital(cnt).CurValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - CurValue", 1);
}

//Output the CurValue for each of the Vitals
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++ ) {
Debug.Log(((VitalName)cnt).ToString() + ": " + pcClass.GetVital(cnt).CurValue);
}
}
}

Any help from Petey or anyone else that solved this problem, is mighty appreciated.

Thanks
-Pat

Offline
Joined: Dec 23 2011

If this helps at all this is the Debug.Log(); output

Health: 1
UnityEngine.Debug:Log(Object)

Energy: 1
UnityEngine.Debug:Log(Object)

Mana: 1
UnityEngine.Debug:Log(Object)

Offline
Joined: Dec 23 2011

right under the comment for getting the value of each vital, replace with.

pcClass.GetVital(cnt).CurValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Cur Value", pcClass.GetVital(cnt).CurValue);
Debug.Log(pcClass.GetVital(cnt).CurValue);
}

Tested and Works! feel free to remove the debug, I just left it there so you can see for yourself.

Offline
Joined: Sep 24 2011

I have the same problem that rdkdwd... and the solution of punk dosn´t work.
regards

Offline
Joined: Apr 21 2012

I'm not sure if this will help but there seems to be a difference between the PlayerPrefs and the pcClass. If you paste this debug code in after the vitals loader for loop you should see conflict.

If your Constitution is set to 120 in your plist your should see both the correct and incorrect output.

for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
Debug.Log(((VitalName)cnt).ToString() + PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Cur Value", 1));
Debug.Log(((VitalName)cnt).ToString() + ": " + pcClass.GetVital(cnt).CurValue);
}