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

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

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

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

Unity

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

Q&A

解決済

1回答

1938閲覧

他のスクリプトから関数を呼び出したいです。

defour

総合スコア21

C#

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

Unity

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

0グッド

0クリップ

投稿2019/11/17 14:06

GameManagerのスクリプトで定義されているA()という関数を、TimeScriptのスクリプトで呼び出したいのですが、うまくいかないです。
イメージ説明
このように、再生するとエラーが生じ
エラー内容は、
NullReferenceException: Object reference not set to an instance of an object
TimeScript.Update () (at Assets/Scripts/TimeScript.cs:37)
とあります。
以下がアクセスするスクリプトです。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class TimeScript : MonoBehaviour 7{ 8 //public GameObject gameobject; 9 GameObject gameobject; 10 11 GameObject limitTimeUI; 12 13 float time = 5.0f; 14 // Start is called before the first frame update 15 void Start() 16 { 17 limitTimeUI = GameObject.Find("LimitTimeUI"); 18 gameobject = GameObject.Find("Time"); 19 //bool B = gameObject.GetComponent<GameManager>().isBallMoving; 20 //Debug.Log(B); 21 22 //B = false; 23 24 //gameobject.GetComponent<GameManager>().ReturnAccess(); 25 } 26 27 // Update is called once per frame 28 void Update() 29 { 30 // 毎フレーム毎に残り時間を減らしていく 31 32 this.time -= Time.deltaTime; 33 if (this.time < 0){ 34 this.limitTimeUI.GetComponent<Text>().text = "Time: 0"; 35 36 GameManager a =GetComponent<GameManager>(); 37 a.A(); 38 } 39 else 40 { 41 // timeを文字列に変換したものをテキストに表示する 42 this.limitTimeUI.GetComponent<Text>().text = "Time: " + this.time.ToString("F0"); 43 } 44 } 45 46 47}

こちらがアクセスされる側のスクリプトです。

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public int StageNo; public bool isBallMoving; /*public void ReturnAccess() { Debug.Log("アクセス成功!!"); }*/ public GameObject ballPrefab; public GameObject ball; public GameObject goButton; public GameObject retryButton; public GameObject clearText; public Collider2D movable1; public Collider2D movable2; public AudioClip clearSE; public AudioClip unmoveSE; public AudioClip moveSE; private AudioSource audioSource; //private Text timeText; //private float time = 30; //ここから追加のコード public HandGestureManager _handGestureManager; public HandControl _hand; // Use this for initialization void Start () { retryButton.SetActive (false); isBallMoving = false; audioSource = gameObject.GetComponent<AudioSource> (); //movable1 = GetComponent<Collider2D>(); //movable2 = GetComponent<Collider2D>(); //GetComponent<Text>().text = ((int)time).ToString(); } // Update is called once per frame void Update () { } public void PushGoButton(){ //if (Input.GetKeyDown(KeyCode.Space)) { //Debug.Log("space"); // } Rigidbody2D rd = ball.GetComponent<Rigidbody2D>(); rd.isKinematic = false; retryButton.SetActive(true); goButton.SetActive(false); isBallMoving = true; movable1.isTrigger = false; movable2.isTrigger = false; } public void PushRetryButton(){ Destroy (ball); ball = (GameObject)Instantiate (ballPrefab); retryButton.SetActive (false); goButton.SetActive (true); isBallMoving = false; } public void PushBackButton(){ GobackStageSelect (); } public void StageClear(){ audioSource.PlayOneShot (clearSE); if (PlayerPrefs.GetInt ("CLEAR", 0) < StageNo) { PlayerPrefs.SetInt ("CLEAR", StageNo); } clearText.SetActive (true); retryButton.SetActive (false); Invoke("GobackStageSelect",3.0f); } void GobackStageSelect(){ SceneManager.LoadScene ("StageSelectScene"); } public void TouchUnMoveBox() { audioSource.PlayOneShot(unmoveSE); } public void TouchMoveBox(){ audioSource.PlayOneShot(moveSE); } public void A() { Rigidbody2D rd = ball.GetComponent<Rigidbody2D>(); rd.isKinematic = false; //retryButton.SetActive(true); //goButton.SetActive(false); isBallMoving = true; movable1.isTrigger = false; movable2.isTrigger = false; } }

なにか必要な情報があれば追記しますので、どなたかアドバイスよろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

timescriptの変数定義で
GameManager g;と宣言し、

Start関数内で
g = GameObjct.Find(“GameManager”).Getcomponent〈GameManager〉();
とします。

その後
g.A();
で呼び出せますよ。

投稿2019/11/17 14:44

編集2019/11/17 14:48
odekakeneko

総合スコア11

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

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

defour

2019/11/20 12:50

返信が遅れました。アドバイス通りしてみると、解決しました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問