Hi all. I just started learning C# on unity. I am following a tutorial and I am having a slight problem. The tutorial is Learn to Code by Making Games by Ben Tristem.
I have been trying to figure out what is the problem with my script. I went back to all the tutorial but I still couldn’t figure out.
I have uploaded the problem.
http://www.gamebucket.io/game/4a79944a-0946-41e1-8ea9-18a165461ecc
If I press start, it goes on correctly. It loads level correctly.
However, when I press start after it loads level 1, if I stay idle (I don’t click anything, balls stay stationary), it jumps straight to the game over scene after a few seconds. I can’t figure out which part of the script is causing this. It’s like there is a “hidden idle timer” if that makes sense.
Hope someone can help me. Thank you.
LevelManager script
using UnityEngine;
using System.Collections;
publicclassLevelManager:MonoBehaviour{
publicvoidLoadScene(stringname){
Application.LoadLevel(name);
}
publicvoidQuitScene(){
Application.Quit();
}
publicvoidbrickDestroyed(){
if(Brickblock.brickCount<=0){
LoadNextLevel();
}
}
publicvoidLoadNextLevel(){
Application.LoadLevel(Application.loadedLevel+1);
}
}
Brickblock script
using UnityEngine;
using System.Collections;
publicclassBrickblock:MonoBehaviour{
publicSprite[]hitSprites;
publicstaticintbrickCount=0;
privateinttimesHits;
privateLevelManagerlevelManager;
privateboolisBreakable;
//Usethisfor initialization
voidStart(){
timesHits=0;
isBreakable=(this.tag=="Breakable");
if(isBreakable){
brickCount++;
}
levelManager=GameObject.FindObjectOfType<LevelManager>();
}
//Updateiscalledonceper frame
voidUpdate(){
}
voidOnCollisionEnter2D(Collision2DCollision){
if(isBreakable){
hitsCounter();
}
}
voidhitsCounter(){
timesHits++;
intmaxHits=hitSprites.Length+1;
if(timesHits>=maxHits){
brickCount--;
levelManager.brickDestroyed();
Destroy(gameObject);
}else{
LoadSprites();
}
}
voidLoadSprites(){
intspriteValues=timesHits-1;
if(hitSprites[spriteValues]){
this.GetComponent<SpriteRenderer>().sprite=hitSprites[spriteValues];
}
}
}
lose collider script
using UnityEngine;
using System.Collections;
publicclassLoseCollider:MonoBehaviour{
privateLevelManagerlevelManager;
voidOnTriggerEnter2D(Collider2DTrigger){
levelManager=GameObject.FindObjectOfType<LevelManager>();
levelManager.LoadScene("Game_Over");
}
voidOnCollisionEnter2D(Collision2DCollision){
print("Collision");
}
}
Ball script
using UnityEngine;
using System.Collections;
publicclassBall:MonoBehaviour{
privatePaddlepaddle;
//Declarevariablesand naming
privateVector3paddleToBallVector;
privateRigidbody2Drb2d;
privateboolGameStart=false;
privatevoidAwake(){
rb2d=GetComponent<Rigidbody2D>();
}
//Usethisfor initialization
voidStart(){
paddle=GameObject.FindObjectOfType<Paddle>();
paddleToBallVector=this.transform.position-paddle.transform.position;
}
//Updateiscalledonceper frame
voidUpdate(){
//Gamehavenot start
if(!GameStart){
this.transform.position=paddle.transform.position+paddleToBallVector;
}
//Gamestartonmouse click
if(Input.GetMouseButtonDown(0)){
this.rb2d.velocity=newVector2(3f,15f);
GameStart=true;
print("MouseClicked");
}
}
}