質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1886閲覧

アイテムボックスのセーブがしたいです。

sho0

総合スコア6

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/05/23 15:39

編集2020/05/24 04:56

前提・実現したいこと

アイテムボックスのセーブがしたいです。

発生している問題・エラーメッセージ

アイテムバックのシーンから、main6のシーンに遷移する時にデータ保存をしたいです。

スタート時にgameobjectのSlotgridにアイテムが入るようになっていて、その後、装備する為に、PlayerSlotにドロップして使用しております。また装備を外す時は、PlayerSlotからSlotgridにドロップしております。
シーンが変わってもこの行き来のセーブがしたいです。

![イメージ説明
イメージ説明
イメージ説明
イメージ説明
イメージ説明

Assets/itemmain6.cs(23,9): error CS0103: The name 'SaveSystem' does not exist in the current context
Assets/itemmain6.cs(22,42): error CS0103: The name 'slotPrefab' does not exist in the current context
Assets/itemmain6.cs(22,21): error CS0103: The name 'SlotGrid' does not exist in the current context
Assets/itemmain6.cs(22,6): error CS0103: The name 'Data' does not exist in the current context
Assets/Scripts/SlotGrid.cs(21,9): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
Assets/save/UserData.cs(12,51): error CS0120: An object reference is required for the non-static field, method, or property 'Component.transform'
Assets/save/UserData.cs(12,39): error CS0103: The name 'slotPrefab' does not exist in the current context

該当のソースコード

itemmain6

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class itemmain6 : MonoBehaviour 7{ 8 // Start is called before the first frame update 9 void Start() 10 { 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 18 } 19 20 public void OnClick() 21 { 22 Data.gotItem = SlotGrid.Instantiate(slotPrefab, this.transform); 23 SaveSystem.Instance.Save(); 24 SceneManager.LoadScene("main6"); 25 } 26 27} 28

UserData

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace WBMap 6{ 7 [System.Serializable] 8public class UserData 9{ 10 public Vector3 Pos =Vector3.zero; 11 public int ihp = 100; 12 public Item gotItem = Instantiate(slotPrefab, SlotGrid.transform); 13} 14}

SlotGrid

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5namespace WBMap{ 6public class SlotGrid : MonoBehaviour 7{ 8[SerializeField] 9private GameObject slotPrefab; 10 11private int slotNumber = 20; 12 13[SerializeField] 14private Item[] allItems; 15 16 // Start is called before the first frame update 17 void Start() 18 { 19 for (int i = 0; i < slotNumber; i++) 20 { 21 Instantiate(slotPrefab, this.transform) = SaveSystem.Instance.UserData.gotItem; 22 23 GameObject slotObj = Instantiate(slotPrefab, this.transform); 24 25 Slot slot = slotObj.GetComponent<Slot>(); 26 27 if (i<allItems.Length) 28 { 29 slot.SetItem(allItems[i]); 30 } 31 else 32 { 33 slot.SetItem(null); 34 } 35 } 36 } 37 38 // Update is called once per frame 39 void Update() 40 { 41 42 } 43} 44} 45
```Hand using System.Collections; using System.Collections.Generic; using UnityEngine; public class Hand : MonoBehaviour { private Item grabbingItem; // Start is called before the first frame update // Update is called once per frame void Update() { this.transform.position = Input.mousePosition; } public Item GetGrabbingItem() { Item oldItem = grabbingItem; grabbingItem = null; return oldItem; } public void SetGrabbingItem( Item item) { grabbingItem = item; } public bool IsHavingItem() { return grabbingItem != null; } }

PlayerSlot

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.EventSystems; 6 7namespace WBMap{ 8 9public class PlayerSlot : Slot 10{ 11 12 13private Player player; 14 15public Player MyPlayer { get => player; private set => player = value; } 16 // Start is called before the first frame update 17 18 protected override void Start() 19 { 20 base.Start(); 21 22 MyPlayer = FindObjectOfType<Player>(); 23 } 24 25 26 27 public override void SetItem(Item item) 28 { 29 MyPlayer.Removeitem(MyItem); 30 MyPlayer.SetItem(item); 31 32 33 34 if (item!=null) 35 { 36 itemImage.color = new Color(1, 1, 1, 1); 37 itemImage.sprite = item.MyItemImage; 38 } 39 else 40 { 41 itemImage.color = new Color(0, 0, 0, 0); 42 } 43 MyItem = item; 44 } 45} 46}
```ここに言語名を入力 ソースコード

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/05/23 22:27

で、しつもんはなんでしょうか
sho0

2020/05/24 04:54

スタート時にgameobjectのSlotgridにアイテムが入るようになっていて、その後、装備する為に、PlayerSlotにドロップして使用しております。また装備を外す時は、PlayerSlotからSlotgridにドロップしております。 シーンが変わってもこの行き来のセーブがしたいです。
guest

回答1

0

ベストアンサー

まずはエラーコードを読みましょう。

・1〜4つ目のエラー
itemmain6クラスにはDataSlotGridslotPrefabSaveSystemが定義されていません。
これが何を意味しているのか分からない(クラス? 変数?)か分からないのでエラーになります。

・5つ目のエラー
SlotGridクラスの以下の部分、左辺と右辺が逆だと思います。
Instantiate(slotPrefab, this.transform) = SaveSystem.Instance.UserData.gotItem;

・6つ目のエラー
UserDataの以下の部分、クラスの定義部(メソッドの外)ではInstantiateは使えません。
public Item gotItem = Instantiate(slotPrefab, SlotGrid.transform);
(ここと5つ目のエラーで同じようなことしているので、その点も意味不明です)

・7つ目のエラー
UserDataクラスにはslotPrefabが定義されていません。(1〜4つ目のエラーと同じ)

上記はそもそもデータ保存云々以前の問題です。
なおシーンをまたいで変数を維持する方法は色々ありますが、とりあえず「unity 変数 シーンをまたぐ」などで調べて自分で試した方法を記載してください。
(質問文には肝心のSaveSystemの内容が記載されていないのでこれ以上回答出来ません)

投稿2020/05/26 06:17

sakura_hana

総合スコア11427

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sho0

2020/05/27 06:27

いつもありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問