###前提・実現したいこと
ボールと壁が衝突したときにその時のボールは消して、新たなボールをインスタンス化して、スペースを押したらまたボールが動き出すようにしたいです。インスタンス化までは実現できているのですが、スペースキーを押しても2個めからは動いてくれません。最初のボールはスペースを押せば動いてくれます。
###発生している問題・エラーメッセージ
MissingComponentException: There is no 'Rigidbody' attached to the "Ball(Clone)" game object, but a script is trying to access it.
You probably need to add a Rigidbody to the game object "Ball(Clone)". Or your script needs to check if the component is attached before using it.
UnityEngine.Rigidbody.get_velocity () (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/Physics/DynamicsBindings.gen.cs:1254)
BallScript.Update () (at Assets/Scripts/BallScript.cs:15)
###該当のソースコード
壁のスクリプト
using UnityEngine;
using System.Collections;
public class ButtomWallScript : MonoBehaviour {
public GameManager gameManager; public GameObject ballPrefab; Vector3 placePosition = new Vector3(0f, 0.6f, -7f); void OnCollisionEnter(Collision collision) { Destroy(collision.gameObject); if (gameManager.life > 0) { Instantiate ( ballPrefab, placePosition, Quaternion.identity ); gameManager.life--; } }
}
ボールのスクリプト
using UnityEngine;
using System.Collections;
public class BallScript : MonoBehaviour {
float speed = 15.0f; void Update () { if(Input.GetButtonUp("Jump") && GetComponent<Rigidbody>().velocity == new Vector3(0, 0, 0)) { GetComponent<Rigidbody>().AddForce((transform.forward + transform.right) * speed, ForceMode.VelocityChange); } } void OnCollisionEnter() { Rigidbody rigidbody = GetComponent<Rigidbody>(); rigidbody.velocity = rigidbody.velocity.normalized * 15; }
}
###試したこと
最初のボールは動きますが、プレハブ化したボールは出現はするのですが、動いてくれません。
アタッチはできているはずです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/08 02:35