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

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

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

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

Unity

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

Q&A

解決済

1回答

2011閲覧

AddListenerが使えない

Serapiss

総合スコア20

C#

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

Unity

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

0グッド

0クリップ

投稿2022/08/23 08:18

編集2022/08/23 09:27

前提
イメージ説明
ボタンを取得と実行をScriptで実行しようとしているのですが、どのサイトでも使われているAddListenerが使えません。
必ず、赤の波線がつけられます。

実現したいこと

  • ボタンを取得と実行をScriptで実行する

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

イメージ説明###

’object’に'AddListener'の定義が含まれておらず。型'object'の最初の引数を受け付ける拡張メソッド'AddListener'が見つかりませんでした。 usingディレクティブまたはアセンブリ参照が不足していないことを確認してください 〇unityにアタッチした時のエラーメッセージ 1>------ ビルド開始: プロジェクト: Assembly-CSharp, 構成: Debug Any CPU ------ 1>C:\Users\naklab\Desktop\sera\TunnelTestProject\Assets\Scenes\MainScript.cs(15,22,15,33): error CS1061: 'object' に 'AddListener' の定義が含まれておらず、型 'object' の最初の引数を受け付ける拡張メソッド 'AddListener' が見つかりませんでした。using ディレクティブまたはアセンブリ参照が不足していないことを確認してください。 1>C:\Users\naklab\Desktop\sera\TunnelTestProject\Assets\Scenes\MainScript.cs(9,20,9,24): warning CS0649: フィールド 'MainScript.test' は割り当てられません。常に既定値 null を使用します。 1>C:\Users\naklab\Desktop\sera\TunnelTestProject\Assets\Scenes\Button.cs(16,21,16,28): warning CS0649: フィールド 'Button.onClick' は割り当てられません。常に既定値 null を使用します。 ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========

該当のソースコード

追加でUnityのオリジナルサイトのコードをコピペしましたが、同様のエラーが発生します。

C#

1using UnityEngine; 2using UnityEngine.UI; 3 4public class Example : MonoBehaviour 5{ 6 //Make sure to attach these Buttons in the Inspector 7 public Button m_YourFirstButton, m_YourSecondButton, m_YourThirdButton; 8 9 void Start() 10 { 11 //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button 12 m_YourFirstButton.onClick.AddListener(TaskOnClick); 13 m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); }); 14 m_YourThirdButton.onClick.AddListener(() => ButtonClicked(42)); 15 m_YourThirdButton.onClick.AddListener(TaskOnClick); 16 } 17 18 void TaskOnClick() 19 { 20 //Output this to console when Button1 or Button3 is clicked 21 Debug.Log("You have clicked the button!"); 22 } 23 24 void TaskWithParameters(string message) 25 { 26 //Output this to console when the Button2 is clicked 27 Debug.Log(message); 28 } 29 30 void ButtonClicked(int buttonNo) 31 { 32 //Output this to console when the Button3 is clicked 33 Debug.Log("Button clicked = " + buttonNo); 34 } 35}

下記のようにコンパクトにまとめてやってみましたが、同様のエラーが発生します。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6 7public class MainScript : MonoBehaviour 8{ 9 private Button test; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 15 test.onClick.AddListener(Test1); //クリック時にbuttonClick()を発火させる 16 17 } 18 19 // Update is called once per frame 20 void Update() 21 { 22 23 } 24 // ボタンを押した時のメソッド 25 public void Test1() 26 { 27 Debug.Log("ボタンをクリックした"); 28 }

C#

1using UnityEngine; 2using UnityEngine.UI; 3 4 5public class MainScript : MonoBehaviour 6{ 7 private Button testButton; 8 private Text buttonText; 9 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 15 testButton = GetComponent<Button>(); 16 buttonText = testButton.transform.GetChild(0).GetComponent<Text>(); 17 buttonText.text = "test1"; 18 testButton.GetComponent<Button>().onClick.AddListener(Test1); 19 20 } 21 22 // Update is called once per frame 23 void Update() 24 { 25 26 27 28 } 29 // ボタンを押した時のメソッド 30 public void Test1() 31 { 32 Debug.Log("ボタンをクリックした"); 33 } 34}

試したこと

・まとめている人のものをコピペして試す
・Getcomponent等分割する

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

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

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

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

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

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

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

m.ts10806

2022/08/23 08:27

エラーメッセージはどちらでしょう。 AddListenr AddListener 出ているエラーはコピペしてください。 コピペできない文字列でしたら画面キャプチャで提示してください。 >https://teratail.com/help/question-tips#questionTips34 >エラーメッセージや実行ログをコピー&ペーストしましょう
Serapiss

2022/08/23 08:31

ご対応ありがとうございます。 失礼いたしました。画像を追加しますので、少々お待ちください。
Serapiss

2022/08/23 08:33

質問の一番上に追加いたしました。
episteme

2022/08/23 08:38

× AddLister 〇 AddListener じゃなくて?
Serapiss

2022/08/23 08:42

AddListenerでエラーが発生します。
drednote

2022/08/23 09:06

VSCodeかなんかでエラーが表示されたということでしょうか? 少なくとも私の環境で上がっているコードをVisualStudioに貼り付けてみて、特段エラーは表示されませんでしたが。 インテリセンスが間に合ってないだけでは?
Serapiss

2022/08/23 09:44 編集

左様でございますか。 Microsoft Visual Studio です。
Serapiss

2022/08/23 09:26

画像を追加したので、ご確認いただけると幸いでございます。
Bongo

2022/08/23 09:54

スクリーンショットを拝見しますに、「MainScript.cs」の他にも「Button.cs」という名称のタブがあるようですが、あれは何でしょうか? もしUnityEngine.UI.Button(https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.UI.Button.html )ではなく独自のスクリプトでしたら、その内容もご提示いただけると手がかりになるかも知れません。
Serapiss

2022/08/23 11:57

ご質問ありがとうございます。独自のスクリプトになります。 内容は以下です。 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Button : MonoBehaviour { public GameObject Panel; public GameObject Panelbutton; public GameObject WaterfallWide; //public GameObject WaterfallDrop; private ParticleSystem.EmissionModule ps_emission; //private ParticleSystem.EmissionModule ps1_emission; int x = 100; int Water = 100; private ParticleSystem ps; internal object onClick; //private ParticleSystem ps1; // Start is called before the first frame update void Start() { ps = WaterfallWide.GetComponent<ParticleSystem>(); //ps1 = WaterfallDrop.GetComponent<ParticleSystem>(); } public void ParticleUp() { var emission = ps.emission; emission.rateOverTime = Water; if (WaterfallWide.activeSelf == false ) { Panel.SetActive(true); Panelbutton.SetActive(true); WaterfallWide.SetActive(true); } else if (WaterfallWide.activeSelf == true) { if (50 > Water) { x = 100; } else if (50 < Water) { x = 0; } } } public void Panelbuttondayo() { Panel.SetActive(false); Panelbutton.SetActive(false); } // Update is called once per frame void Update() { var emission = ps.emission; //var emission1 = ps1.emission; emission.rateOverTime = Water; //emission1.rateOverTime = Water; if (x > Water) { Water = Water + 1; } else if (x < Water) { Water = Water - 1; } Debug.Log("Water"); Debug.Log(Water); Debug.Log("x"); Debug.Log(x); ps_emission = ps.emission; ps_emission.rateOverTime = new ParticleSystem.MinMaxCurve(Water); //ps1_emission = ps1.emission; //ps1_emission.rateOverTime = new ParticleSystem.MinMaxCurve(Water); if (Input.GetKey(KeyCode.Space)) { WaterfallWide.SetActive(true); } } }
Serapiss

2022/08/23 12:01

project自体も新しいものを作ってそこで実行したら、動きました。 まったく理由がわかりません。
drednote

2022/08/23 12:05

名前空間的にどっちが優先されるのかわかりませんが、今回このMonoBehaviourのButtonがUnityEngine.UIの物より優先されてるんじゃないです? Buttonってクラス名じゃなく全然違う名前にしたらOKだと思うんですけど
Serapiss

2022/08/23 12:19

Butoonのsc名を変更したら、解決しました!!! 本当に本当にありがとうございます。 今週末までに、実験装置を完成させないといけない状況にあり、絶望していたのですが助かりました! 対応していただいた、皆様誠にありがとうございました。
Serapiss

2022/08/23 12:21

もし、お手数でなければ上記内容の内容を回答欄に書いていただけると幸いでございます。
m.ts10806

2022/08/23 22:57

質問者も回答できます。 自身で理解した内容をまとめて回答とされたほうが後から見る人にも伝わりやすくなるかもしれません。 こちらのコメントに書く場合は「ちょっと試してみて」レベルで回答にするほどでもない内容であることが多いです。
Serapiss

2022/08/24 02:21

ご丁寧にありがとうございます!そうします!
guest

回答1

0

自己解決

Button.csというスクリプトファイルが別に作成されていたため、おそらくそちらが優先されてしまった。

投稿2022/08/24 02:22

Serapiss

総合スコア20

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問