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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

1128閲覧

unity C#でほかのスクリプトの変数を取得したいが、コンテキストに存在しませんとエラーが出る。

lastlast

総合スコア67

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

0クリップ

投稿2022/06/09 14:07

前提

unityのほかのスクリプトの変数を取得したいのですが、うまくいきません。
https://obenkyolab.com/?p=2294
https://yttm-work.jp/unity/unity_0012.html
https://htsuda.net/archives/1702
これらのサイトを参考にして、以下のようなコードを作ってみました。

C#

1Debug.Log(Cinemachine.CinemachineDollyCart ,m_Speed);

これで、CinemachineDollyCart.csというファイル内のm_Speedという変数を取得できるだろうと思っていたのですが、できませんでした。

エラーメッセージ
error CS0119: 'CinemachineDollyCart' is a type, which is not valid in the given context error CS0103: The name 'm_Speed' does not exist in the current context

なお、CinemachineDollyCart.csのコードは以下のようになっています。

C#

1using UnityEngine; 2using UnityEngine.Serialization; 3 4namespace Cinemachine 5{ 6 /// <summary> 7 /// This is a very simple behaviour that constrains its transform to a CinemachinePath. 8 /// It can be used to animate any objects along a path, or as a Follow target for 9 /// Cinemachine Virtual Cameras. 10 /// </summary> 11 [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)] 12 [ExecuteAlways] 13 [DisallowMultipleComponent] 14 [HelpURL(Documentation.BaseURL + "manual/CinemachineDollyCart.html")] 15 public class CinemachineDollyCart : MonoBehaviour 16 { 17 /// <summary>The path to follow</summary> 18 [Tooltip("The path to follow")] 19 public CinemachinePathBase m_Path; 20 21 /// <summary>This enum defines the options available for the update method.</summary> 22 public enum UpdateMethod 23 { 24 /// <summary>Updated in normal MonoBehaviour Update.</summary> 25 Update, 26 /// <summary>Updated in sync with the Physics module, in FixedUpdate</summary> 27 FixedUpdate, 28 /// <summary>Updated in normal MonoBehaviour LateUpdate</summary> 29 LateUpdate 30 }; 31 32 /// <summary>When to move the cart, if Velocity is non-zero</summary> 33 [Tooltip("When to move the cart, if Velocity is non-zero")] 34 public UpdateMethod m_UpdateMethod = UpdateMethod.Update; 35 36 /// <summary>How to interpret the Path Position</summary> 37 [Tooltip("How to interpret the Path Position. If set to Path Units, values are as follows: 0 represents the first waypoint on the path, 1 is the second, and so on. Values in-between are points on the path in between the waypoints. If set to Distance, then Path Position represents distance along the path.")] 38 public CinemachinePathBase.PositionUnits m_PositionUnits = CinemachinePathBase.PositionUnits.Distance; 39 40 /// <summary>Move the cart with this speed</summary> 41 [Tooltip("Move the cart with this speed along the path. The value is interpreted according to the Position Units setting.")] 42 [FormerlySerializedAs("m_Velocity")] 43 public float m_Speed; 44 45 /// <summary>The cart's current position on the path, in distance units</summary> 46 [Tooltip("The position along the path at which the cart will be placed. This can be animated directly or, if the velocity is non-zero, will be updated automatically. The value is interpreted according to the Position Units setting.")] 47 [FormerlySerializedAs("m_CurrentDistance")] 48 public float m_Position; 49 50 void FixedUpdate() 51 { 52 if (m_UpdateMethod == UpdateMethod.FixedUpdate) 53 SetCartPosition(m_Position + m_Speed * Time.deltaTime); 54 } 55 56 void Update() 57 { 58 float speed = Application.isPlaying ? m_Speed : 0; 59 if (m_UpdateMethod == UpdateMethod.Update) 60 SetCartPosition(m_Position + speed * Time.deltaTime); 61 } 62 63 void LateUpdate() 64 { 65 if (!Application.isPlaying) 66 SetCartPosition(m_Position); 67 else if (m_UpdateMethod == UpdateMethod.LateUpdate) 68 SetCartPosition(m_Position + m_Speed * Time.deltaTime); 69 } 70 71 void SetCartPosition(float distanceAlongPath) 72 { 73 if (m_Path != null) 74 { 75 m_Position = m_Path.StandardizeUnit(distanceAlongPath, m_PositionUnits); 76 transform.position = m_Path.EvaluatePositionAtUnit(m_Position, m_PositionUnits); 77 transform.rotation = m_Path.EvaluateOrientationAtUnit(m_Position, m_PositionUnits); 78 } 79 } 80 } 81} 82

試したこと

検索等をした

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

どうしたらよいのでしょうか。
質問に不備があればコメントで教えてください。
かれこれ3時間ほど悩んでいます。
どうかよろしくお願いいたします。

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

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

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

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

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

bboydaisuke

2022/06/09 22:02

インスタンスを取得しなければプロパティは取得できませんよ。
guest

回答1

0

ベストアンサー

public float m_Speed;

ってのはインスタンス変数なので、そのクラスのインスタンスを介してアクセスする必要があります

投稿2022/06/09 23:47

y_waiwai

総合スコア87749

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問