前提・実現したいこと
https://tech.pjin.jp/blog/2015/09/13/unity%e3%81%a7%e6%a8%aa%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%82%a2%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%b2%e3%83%bc%e3%83%a0%e3%82%92%e4%bd%9c%e3%82%8d%e3%81%86%e2%91%a4-%ef%bd%9e/
を写しながら、Unityでゲームを作成しています。その際にエラーが発生しました。
発生している問題・エラーメッセージ
MissingComponentException: There is no 'Renderer' attached to the "GameController" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "GameController". Or your script needs to check if the component is attached before using it. UnityEngine.Renderer.get_sharedMaterial () (at <ca496b8c93454b2f9b9924292c19379f>:0) GameController.Update () (at Assets/Script/GameController.cs:39)
該当のソースコード
GameController
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class GameController : MonoBehaviour { private GameObject player; private GameObject start; private GameObject gameOver; public static bool isPlaying; public float scroll = 0.15f; Renderer cRenderer; void Awake() { player = GameObject.Find("Player"); start = GameObject.Find("Start"); gameOver = GameObject.Find("GameOver"); cRenderer = GetComponent<Renderer>(); } void Start() { gameOver.SetActive(false); isPlaying = false; } void Update() { if (Input.GetKeyDown("a")) { SceneManager.LoadScene("bird"); } float x = Mathf.Repeat(Time.time * scroll, 1); Vector2 offset = new Vector2(x, 0); cRenderer.sharedMaterial.SetTextureOffset("_MainTex", offset); } public void GameStart() { start.SetActive(false); isPlaying = true; } public void Stop() { Destroy(this); } public void GameOver() { if (isPlaying == true) { gameOver.SetActive(true); isPlaying = false; player.GetComponent<Player>().enabled = false; } } }
Wall
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Wall : MonoBehaviour { public float scroll = 0.15f; Renderer cRenderer; void Awake() { cRenderer = GetComponent<Renderer>(); } void Update() { float x = Mathf.Repeat(Time.time * scroll, 1); Vector2 offset = new Vector2(x, 0); cRenderer.sharedMaterial.SetTextureOffset("_MainTex", offset); } public void Stop() { Destroy(this); } }
SpawnPoint
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpawnPoint : MonoBehaviour { public GameObject spawnObject; public float interval; void Update() { transform.position = new Vector3(transform.position.x, Random.Range(5f, 30f), transform.position.z); } public void StartSpawn() { StartCoroutine("SpawnWalls"); } public void StopSpawn() { StopCoroutine("SpawnWalls"); } IEnumerator SpawnWalls() { while (true) { Instantiate(spawnObject, transform.position, transform.rotation); yield return new WaitForSeconds(interval); } } }
試したこと
エラーの翻訳
Scrollの値を変える
スクリプトを入れる場所を変えてみる
補足情報
現在、同じ所にWallオブジェクトが生成されているようです。
床はplayerと一緒に問題なく動いています。
WallのcRenderer = GetComponent<Renderer>();
でデバッグしたところ、cRendererがnullとのことでした。
WallスクリプトとSpawnPointスクリプトはWallオブジェクト、GameControllerスクリプトはGameControllerという名前のEmptyオブジェクトに入っています。
まだ回答がついていません
会員登録して回答してみよう