@bloodrosesoll2 (kroro)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

スコアをセーブしてゲーム再起動時にも使いたい!

解決したいこと

スコアのセーブロード

開発中のゲームのスコアのセーブロードがしたいのですが、いろんな動画や記事を見ましたがセーブとロードが結局分からず困っています。
一応書いたコードを貼っておきますので解決できる方がいましたらご教授ください。

やりたいことはスコア5000を超えたらそれがセーブされ、次に始める時に使えるみたいな感じです。
一応書いてみましたがエラーが出ていて使えません。

発生している問題・エラー

Assets\Fungus\Scripts\Fera\GameController.cs(21,26): error CS0029: Cannot implicitly convert type 'int' to 'string'

該当するソースコード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{

public Text scoreText;

private static int score;



// Start is called before the first frame update
void Start()
{
    
    scoreText.text = PlayerPrefs.GetInt("Sensitivity", score);




}

// Update is called once per frame
public void AddScore()
{
    score += 100;
    scoreText.text = "Sensitivity" + score;
    if (score > 5000)
    {

        PlayerPrefs.SetInt("Sensitivity", score);
        PlayerPrefs.Save();



    
    }
    void OnDestroy()
    {
        PlayerPrefs.SetInt("Sensitivity", score);
        PlayerPrefs.Save();
    }
}

}

0 likes

2Answer

scoreText.text = PlayerPrefs.GetInt("Sensitivity", score);

intで渡しているからではないでしょうか?

1Like

いい記事を見つけたのでそれをいじってみたらスコアの保存が出来ましたので一応解決しました。
一応コードを記載しておきます↓

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class score : MonoBehaviour
{
public GameObject score_object = null; // Textオブジェクト
public int score_num = 0; // スコア変数

// 初期化時の処理
void Start()
{
    //PlayerPrefs.DeleteKey("SCORE");
    // スコアのロード
    score_num = PlayerPrefs.GetInt("SCORE", 0);
}
// 削除時の処理
void OnDestroy()
{
    // スコアを保存
    PlayerPrefs.SetInt("SCORE", score_num);
    PlayerPrefs.Save();
}

// 更新
public void AddScore()
{
    // オブジェクトからTextコンポーネントを取得
    Text score_text = score_object.GetComponent<Text>();
    // テキストの表示を入れ替える
    score_text.text = "Score:" + score_num;

    score_num += 100; // とりあえず1加算し続けてみる
    if (score_num > 5000)
    {

        //PlayerPrefs.DeleteKey("SCORE");



        //score_num = 10;
    }
}

}

0Like

Your answer might help someone💌