前提
ボタンを取得と実行を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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
エラーメッセージはどちらでしょう。
AddListenr
AddListener
出ているエラーはコピペしてください。
コピペできない文字列でしたら画面キャプチャで提示してください。
>https://teratail.com/help/question-tips#questionTips34
>エラーメッセージや実行ログをコピー&ペーストしましょう
ご対応ありがとうございます。
失礼いたしました。画像を追加しますので、少々お待ちください。
質問の一番上に追加いたしました。
× AddLister
〇 AddListener
じゃなくて?
AddListenerでエラーが発生します。
VSCodeかなんかでエラーが表示されたということでしょうか?
少なくとも私の環境で上がっているコードをVisualStudioに貼り付けてみて、特段エラーは表示されませんでしたが。
インテリセンスが間に合ってないだけでは?
左様でございますか。
Microsoft Visual Studio です。
画像を追加したので、ご確認いただけると幸いでございます。
スクリーンショットを拝見しますに、「MainScript.cs」の他にも「Button.cs」という名称のタブがあるようですが、あれは何でしょうか?
もしUnityEngine.UI.Button(https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.UI.Button.html )ではなく独自のスクリプトでしたら、その内容もご提示いただけると手がかりになるかも知れません。
ご質問ありがとうございます。独自のスクリプトになります。
内容は以下です。
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);
}
}
}
project自体も新しいものを作ってそこで実行したら、動きました。
まったく理由がわかりません。
名前空間的にどっちが優先されるのかわかりませんが、今回このMonoBehaviourのButtonがUnityEngine.UIの物より優先されてるんじゃないです?
Buttonってクラス名じゃなく全然違う名前にしたらOKだと思うんですけど
Butoonのsc名を変更したら、解決しました!!!
本当に本当にありがとうございます。
今週末までに、実験装置を完成させないといけない状況にあり、絶望していたのですが助かりました!
対応していただいた、皆様誠にありがとうございました。
もし、お手数でなければ上記内容の内容を回答欄に書いていただけると幸いでございます。
質問者も回答できます。
自身で理解した内容をまとめて回答とされたほうが後から見る人にも伝わりやすくなるかもしれません。
こちらのコメントに書く場合は「ちょっと試してみて」レベルで回答にするほどでもない内容であることが多いです。
ご丁寧にありがとうございます!そうします!

回答1件
あなたの回答
tips
プレビュー