- Home /
C# how to save game when player picks up an object?
Hey guys,
i am working a a script for the player where it saves the game every time they pick up an object. And the player can exit the game and come back to it later and click continue and it will load with the last save from when they picked up the last object.
So, this is what i have so far, I could use some help with the save function and the load function, and if there is anything else wrong with the script, all help is welcomed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
{
public GameObject CollectableObject //object to be picked up
public Ridgidbody Player //player that can pick up the object
private void Update ()
{
If Player = GetKeyDown “E” + CollectableObject //if the player presses “E” and they collect the game object, then the game object is set inactive and the game is saved
{
Set active CollectableObject (False);
Save PlayerPrefs;
}
If UI (OnClick) = “Continue” //If the player clicks on the UI button that is labeled “Continue, the game will load the last save game from the player prefs.
{
Load PlayerPrefs;
}
}
}
Their is likely more than one issue with this code, anyone who can help me out, it is really appreciated
Thank you.
Answer by Topthink · Apr 09, 2018 at 01:37 AM
I wonder if putting a collider on the object and then attaching the code to the "collision" would work in your case.
Thank you, I am trying that now, I will let you know how it goes.
Answer by RocketFriday · Apr 10, 2018 at 01:35 AM
With Events, simply make an event for points when you want to save (save game button or checkpoint) and trigger the event as well when an object is picked up.
Answer by Cynikal · Apr 04, 2018 at 10:20 PM
Look into PlayerPrefs for a simple solution. You can save the data, and pull it on the next load.
thank you for telling me what i already know. I know that i am supposed to use player prefs, i just don't know how to use them properly, and was asking for assistance on the code itself. I do not appreciate the unhelpful comment.
Your answer
Follow this Question
Related Questions
My shop system script is not working 2 Answers
How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer