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

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

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

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

Unity

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

Q&A

解決済

1回答

5262閲覧

unityで関数が呼び出せない。

nakatsu6723

総合スコア38

C#

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

Unity

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

0グッド

0クリップ

投稿2016/12/04 08:09

編集2016/12/04 08:45

###前提・実現したいこと
unityでゲームアプリを作っているのですが、他のファイルで作った関数が呼び出せません。
中身は、AppleかBomb(Tag名)がbasketと衝突したら、GetApple関数かGetBomb関数を呼んでUIに反映させるアプリです。
GameDirector.csにあるGetApple関数とGetBomb関数をBasketController.csで呼ぼうとしているのですが、エラーが出てしまいます。
色々試した結果、関数の呼び出しそのものがうまくいってないようです。

エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object
BasketController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/BasketController.cs:18)

###該当のソースコード

BasketController.csのソースコード

using UnityEngine;
using System.Collections;

public class BasketController : MonoBehaviour {

public AudioClip appleSE; public AudioClip bombSE; AudioSource aud; GameObject director; void Start () { this.director = GameObject.Find("GameDirector"); this.aud = GetComponent<AudioSource>(); } void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "Apple") { this.director.GetComponent<GameDirector>().GetApple(); this.aud.PlayOneShot(this.appleSE); } else { this.director.GetComponent<GameDirector>().GetBomb(); this.aud.PlayOneShot(this.bombSE); } Destroy(other.gameObject); } // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit, Mathf.Infinity)) { float x = Mathf.RoundToInt(hit.point.x); float z = Mathf.RoundToInt(hit.point.z); transform.position = new Vector3(x, 0.0f, z); } } }

}

GameDirector.csのソースコード

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GameDirector : MonoBehaviour {

GameObject timerText; GameObject pointText; float time = 60.0f; int point = 0; public void GetApple() { this.point += 100; } public void GetBomb() { this.point /= 2; } // Use this for initialization void Start () { this.timerText = GameObject.Find("Time"); this.pointText = GameObject.Find("Point"); } // Update is called once per frame void Update () { this.time -= Time.deltaTime; this.timerText.GetComponent<Text>().text = this.time.ToString("F1"); this.pointText.GetComponent<Text>().text = this.point.ToString() + "point"; }

}

###試したこと
衝突判定まではうまくいっています。
エラーメッセージに表示されているのはthis.director.GetComponent<GameDirector>().GetApple();の部分ですが、bombと衝突したときもうまくいきません。

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2016/12/04 09:06 編集

このスクリプトのStart関数が呼ばれる時点で、"GameDirector"のオブジェクトがインスタンス化されてることは確認済みでしょうか?また、GameObject.Findの引数のスペリングに大文字小文字などの間違いはありませんか?
nakatsu6723

2016/12/04 09:48

GameObject.Findの引数とシーンで配置したGameDirectorの名前が一致していないことが原因でした。ご指摘ありがとうございます!
guest

回答1

0

自己解決

GameObject.Findの引数とシーンで配置したGameDirectorの名前が一致していないことが原因でした。

投稿2016/12/04 09:51

nakatsu6723

総合スコア38

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問