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

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

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

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

Unity

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

Q&A

解決済

1回答

675閲覧

Unity C#構造体のCS0246についての質問

kikik

総合スコア24

C#

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

Unity

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

0グッド

0クリップ

投稿2020/03/11 15:09

編集2020/03/11 15:32

前提・実現したいこと

kouzoutai.csで構造体を作成し数を入れて、serect.csに送ろうとしたら
以下のエラーメッセージが発生しました。どうすればserect.csに送れるか教えてください。

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

Assets\scripts\serect.cs(18,9): error CS0246: The type or namespace name 'status' could not be found (are you missing a using directive or an assembly reference?)

該当のソースコード

C#

1/*kouzoutai.csのソースコード*/ 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class kouzoutai : MonoBehaviour 7{ 8 public struct status 9 { 10 public string name; 11 public double HP; 12 public double SP; 13 public double str; 14 public double def; 15 public double dex; 16 public double mid; 17 public double cg; 18 public double luc; 19 public int[] cbuki; 20 } 21 22 public status kongou = new status { name = "金剛", HP = 134, SP = 21, str = 29, def = 23, dex = 3, mid = 31, cg = 24, luc = 28, cbuki = new int[8] { 0, 0, 0, 1, 0, 0, 1, 0 } }; 23 24 25 // Start is called before the first frame update 26 void Start() 27 { 28 } 29 void Update() 30 {} 31}

C#

1/*serect.csのソースコード*/ 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class serect : MonoBehaviour 7{ 8 public int scturn = 1; 9 public int cs = 1; 10 public int[] phai; 11 public kouzoutai kouzoutai; 12 13 // Start is called before the first frame update 14 void Start() 15 { 16 Debug.Log(cs); 17 phai = new int[4]; 18 19 status kongou = kouzoutai.kongou;/*ココ*/ 20 21 } 22 void Update() 23 { 24 25 } 26}

試したこと

kouzoutai.csのソースコードにある
public struct status
{
public string name;
public double HP;
public double SP;
public double str;
public double def;
public double dex;
public double mid;
public double cg;
public double luc;
public int[] cbuki;
}
をserect.csの11行目にも書いたのですが今度は、
Assets\scripts\serect.cs(30,26): error CS0029: Cannot implicitly convert type 'kouzoutai.status' to 'serect.status'と言うエラーが出ました

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

/ココ/と書いてあるところのstatusがエラーの元です。

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

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

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

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

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

guest

回答1

0

ベストアンサー

構造体をクラスの内部で定義してしまうと、そのクラス内でしか使えなくなります。
クラスの外に出してしまえば、この問題は解決すると思います。

C#

1/*kouzoutai.csのソースコード*/ 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public struct status 7{ 8 public string name; 9 public double HP; 10 public double SP; 11 public double str; 12 public double def; 13 public double dex; 14 public double mid; 15 public double cg; 16 public double luc; 17 public int[] cbuki; 18} 19 20public class kouzoutai : MonoBehaviour 21{ 22 public status kongou = new status { name = "金剛", HP = 134, SP = 21, str = 29, def = 23, dex = 3, mid = 31, cg = 24, luc = 28, cbuki = new int[8] { 0, 0, 0, 1, 0, 0, 1, 0 } }; 23 24 25 // Start is called before the first frame update 26 void Start() 27 { 28 } 29 void Update() 30 { } 31}

投稿2020/03/11 16:13

nskydiving

総合スコア6500

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

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

kikik

2020/03/11 17:10 編集

おかげで解決しました。ありがとうございます。
kikik

2020/03/11 16:47

上にあったエラーは言うとおりにしたら消えました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問