Hi everyone. I am new here. I just started learning scripting C# on Unity not long ago.
Currently, I am following along a tutorial but I am having a major problem that I cannot seems to solve.I have posted on Unity forum, but there isn't any response yet.
Hope someone can help me here. Thank you very much,
I have uploaded my game on gamebucket.
http://www.gamebucket.io/game/4a79944a-0946-41e1-8ea9-18a165461ecc
The game is currently script to Autoplay, so it doesn't matter.
I have 1 problem. If I click start on the first screen, it will take me to the "first level". no problem. If I left mouse click, the ball will move. no problem.
However, if I clicked start and move to the next screen, "first level" but I don't click on the mouse. I let the ball stationary. If I stay idle for a few seconds, it will take me to the Game Over screen.
I look through the tutorial 3 times and I still cannot find the problem.
Is there an "idle timer" that I have placed accidentally between the codes?
I have zip the scripts into a zip file. The zip file only contains all scripts that are involved in this project and nothing else.
I will also paste the code here if you do not wish to download the zip folder.
LevelManager.cs = Responsible for loading the levels, win and game over scene,
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class LevelManager : MonoBehaviour {
public void LoadScene(string name){
Brickblock.brickCount = 0;
SceneManager.LoadScene (name);
}
public void QuitScene(){
Application.Quit ();
}
public void brickDestroyed(){
if (Brickblock.brickCount <= 0) {
LoadNextLevel ();
}
}
public void LoadNextLevel(){
Brickblock.brickCount = 0;
SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex + 1);
}
}
Ball.cs = Responsible for the moving ball on every level.
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public AudioSource soundSource;
private Paddle paddle;
//Declare variables and naming
private Vector3 paddleToBallVector;
private Rigidbody2D rb2d;
private bool GameStart=false;
private void Awake (){
rb2d = GetComponent<Rigidbody2D> ();
}
// Use this for initialization
void Start () {
paddle = GameObject.FindObjectOfType<Paddle> ();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update () {
//Game have not start
if (!GameStart) {
this.transform.position = paddle.transform.position + paddleToBallVector;
}
//Game start on mouse click
if (Input.GetMouseButtonDown (0)) {
this.rb2d.velocity = new Vector2 (3f, 15f);
GameStart = true;
}
}
void OnCollisionEnter2D (Collision2D Collision){
soundSource = GetComponent<AudioSource> ();
Vector2 tweak = new Vector2 (Random.Range (0f, 0.2f), Random.Range (0f, 0.2f));
if (GameStart) {
soundSource.Play();
this.rb2d.velocity += tweak;
}
}
}
Brickblock = Responsible for the blocks to be hit.
using UnityEngine;
using System.Collections;
public class Brickblock : MonoBehaviour {
public Sprite[] hitSprites;
public static int brickCount = 0;
public GameObject particlesVfx;
private int timesHits;
private LevelManager levelManager;
private bool isBreakable;
// Use this for initialization
void Start () {
timesHits = 0;
isBreakable = (this.tag == "Breakable");
if (isBreakable) {
brickCount++;
}
levelManager = GameObject.FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D Collision){
if (isBreakable) {
hitsCounter ();
}
}
void hitsCounter () {
timesHits++;
int maxHits = hitSprites.Length + 1;
if (timesHits >= maxHits) {
brickCount--;
levelManager.brickDestroyed ();
particlesEffect ();
Destroy (gameObject);
} else {
LoadSprites ();
}
}
void particlesEffect (){
GameObject particlesVfxObject = Instantiate (particlesVfx, transform.position, Quaternion.identity) as GameObject;
particlesVfx.GetComponent<ParticleSystem> ().startColor = gameObject.GetComponent<SpriteRenderer> ().color;
}
void LoadSprites (){
int spriteValues = timesHits - 1;
if (hitSprites [spriteValues]) {
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteValues];
}
}
}
LoseCollider= Responsible for the collision to occur when the ball drop below the screen.
using UnityEngine;
using System.Collections;
public class LoseCollider : MonoBehaviour {
private LevelManager levelManager;
void OnTriggerEnter2D (Collider2D Trigger){
levelManager = GameObject.FindObjectOfType<LevelManager> ();
levelManager.LoadScene ("Game_Over");
}
void OnCollisionEnter2D (Collision2D Collision){
print ("Collision");
}
}
Paddle = Responsible for the movement of the moving brick to deflect the ball upwards.
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public bool autoPlay=false;
private Ball ball;
// Use this for initialization
void Start () {
ball = GameObject.FindObjectOfType<Ball> ();
}
// Update is called once per frame
void Update () {
if (!autoPlay) {
MouseMovement ();
} else {
AutoPlay ();
}
}
void MouseMovement () {
Vector3 paddlePos = new Vector3 (0.5f, transform.position.y, 0f);
float mousePosInUnits = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp (mousePosInUnits, 1f, 15f);
transform.position = paddlePos;
}
void AutoPlay () {
Vector3 paddlePos = new Vector3 (0.5f, transform.position.y, 0f);
Vector3 ballPos = ball.transform.position;
paddlePos.x = Mathf.Clamp (ballPos.x, 1f, 15f);
transform.position = paddlePos;
}
}
BGMusic.cs = Respobsible for the BGmusic.
using UnityEngine;
using System.Collections;
public class BGMusic : MonoBehaviour {
static BGMusic instance = null;
// Use this for initialization
void Start () {
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
GameObject.DontDestroyOnLoad (gameObject);
}
}
// Update is called once per frame
void Update () {
}
}