棒を積み上げていくというゲームを作っているのですが、棒を動かすというところでうまくいかずに困っています。
StickGeneratorで作った棒をMoveButtonの入力によってStickControllerで動かしているのですが、ClickLeft()だけUnassignedReferenceException: The variable now of StickGenerator has not been assigned.と出てしまって左方向にだけ動きません。
調べてみた結果初期化していない変数を使っているなどで出るみたいですが、左方向だけ動かないという点で何が原因なのかわかりません。
よろしくお願いします。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StickGenerator : MonoBehaviour { public GameObject Stick; public GameObject now; GameObject director; // Use this for initialization void Start () { now = Instantiate(Stick) as GameObject; this.director = GameObject.Find("GameDirector"); } // Update is called once per frame void Update () { } public void CreateStick() { now = Instantiate(Stick,new Vector3(0,5.0f + director.GetComponent<GameDirector>().score),Quaternion.identity) as GameObject; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StickController : MonoBehaviour { float speedX = 0; float speedZ = 0; GameObject Generator; // Use this for initialization void Start () { this.Generator = GameObject.Find("StickGenerator"); } // Update is called once per frame void Update () { Generator.GetComponent<StickGenerator>().now.transform.Translate(this.speedX, 0,this.speedZ); this.speedX *= 0.96f; this.speedZ *= 0.96f; } //右へ動かす public void MoveRight() { this.speedX = 0.01f; } //左へ動かす public void MoveLeft() { this.speedX = -0.01f; } //手前へ動かす public void MoveFront() { this.speedZ = -0.01f; } //奥へ動かす public void MoveBack() { this.speedZ = 0.01f; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveButton : MonoBehaviour { GameObject stick; GameObject Generator; // Use this for initialization void Start () { this.Generator = GameObject.Find("StickGenerator"); this.stick = Generator.GetComponent<StickGenerator>().now; } // Update is called once per frame void Update () { } public void ClickRight() { stick.GetComponent<StickController>().MoveRight(); } public void ClickLeft() { stick.GetComponent<StickController>().MoveLeft(); } public void ClickFront() { stick.GetComponent<StickController>().MoveFront(); } public void ClickBack() { stick.GetComponent<StickController>().MoveBack(); } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/02 03:15 編集
2018/05/02 03:28
2018/05/02 03:42