UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionMy help topic EmptyMy help topic

more_horiz
Well to cut it short, it's a topic about all of my problems that I've encountered.
All problems that I have encountered before will be listed here to let the public to refer to my topic. It also prevents me from creating multiple help topics (which can be very annoying and troublesome at times).

Therefore, all my future questions will be posted on this topic.

List of problems :


Currently, I'm not experiencing any problems while making my first game (for the saving problems, that part was solved), so I will post up one if I have any soon.

descriptionMy help topic EmptyRe: My help topic

more_horiz
Problem #1

I need help on making the soundtrack to continue playing when a game/level is restarted. However, it plays a different soundtrack at the next level. The question is, how do I actually script it? Though I understand the use of DontDestroyOnLoad, but then, I didn't know what I was doing throughout the process, and got this error: http://prntscr.com/a3w532

I created a new C# script to manage the game level's soundtracks, but still the lack of experience in C# pushed me into this state.

BGM C# Script

Code:


using UnityEngine;
using System.Collections;

public class BGM : MonoBehaviour {

   public AudioClip[] audioClip;
    public GameManager manager;
     public bool usesManager = true;
    public int currentLevel;
   
   void Start () {


    if(usesManager)
      {
      manager = manager.GetComponent<GameManager>();
        }



    if(manager.currentLevel == 0)
      {
         PlaySound(0);
       }
   }
   
   void Awake () {

            //  DontDestroyOnLoad(this.gameObject);
   
   }

   void PlaySound(int clip)
   {
      audio.clip = audioClip[clip];
      audio.Play();
   }
}




GameManager Script ( yeah ignore those comments as I suck at labeling variables)

Code:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {


//count
   public int currentScore;
   public int highscore;
   public int currentLevel = 0;
   public int unlockedLevel;
    public int tokenCount;
    private int totaltokenCount;

    public int Levelnow;

//timer variables   
   public float startTime;
   private string currentTime;
   public Rect timerRect;
   public Color warningColorTimer;
    public Color defaultColorTimer;

//GUI Skins
    public GUISkin skin;
//references
    public GameObject tokenParent;
    private bool completed = false;
    private bool showWinScreen = false;
    public int winScreenWidth, winScreenHeight;

    private bool showPauseScreen = false;
    public int pauseScreenWidth, pauseScreenHeight;
    public AudioClip[] audioClip;
    private bool showTimeUpScreen = false;

 


   void Update()
   {

      if(!completed)
      {

      startTime -= Time.deltaTime;
      currentTime = string.Format("{0:0.0}", startTime);
      if(startTime <= 0)
      {
         startTime = 0;
         showTimeUpScreen = true;
      }
        }


        if(Input.GetButtonDown("Pause"))
       {
                  Time.timeScale = 0f;
                  showPauseScreen = true;
        }



   }

   void Start()
   {
      totaltokenCount = tokenParent.transform.childCount;

      if(PlayerPrefs.GetInt("Level Completed") > 0)
      {
       currentLevel = PlayerPrefs.GetInt("Level Completed");


      }else
      {
         currentLevel = 0;
      }
      //DontDestroyOnLoad(gameObject);

   }

   public  void CompleteLevel()
   {
      showWinScreen = true;
      completed = true;   
    }

    void LoadNextLevel()
    {
       Time.timeScale = 1f;
        if (currentLevel < 3)
      {
        currentLevel += 1;
        print (currentLevel);
        SaveGame();
      Application.LoadLevel(currentLevel );
      }
      else{

         print("You Win!!!");
      
           }
    }

    void SaveGame()
    {


        PlayerPrefs.SetInt("Level Completed", (currentLevel + 1));
      PlayerPrefs.SetInt("Level" + currentLevel.ToString() + "Score", currentScore);
   }

   

   void OnGUI()
   {
      GUI.skin = skin;

      if (startTime < 5f)
      {
         skin.GetStyle("Timer").normal.textColor = warningColorTimer;
      }
      else{
         skin.GetStyle("Timer").normal.textColor = defaultColorTimer;
         }

      GUI.Label (timerRect, currentTime, skin.GetStyle("Timer"));
      GUI.Label (new Rect(45,100,200,200),tokenCount.ToString() + "/" + totaltokenCount.ToString());


      if(showWinScreen)
      {
         Rect winScreenRect = new Rect(Screen.width/2 - (Screen.width*.5f/2), Screen.height/2 - (Screen.height*.5f/2), Screen.width*.5f , Screen.height*.5f);
         GUI.Box(winScreenRect, "You Won!!");
           
            int gameTime = (int)startTime;
            currentScore = tokenCount * gameTime;
            if(GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - 170, winScreenRect.y + winScreenRect.height -60, 150, 45), "Continue"))
            {LoadNextLevel();}
            if(GUI.Button(new Rect(winScreenRect.x + 20, winScreenRect.y + winScreenRect.height -60, 100, 45), "Quit"))
            {
               Application.LoadLevel("Main Menu");
               Time.timeScale = 1f;
               SaveGame();

            }

            GUI.Label(new Rect(winScreenRect.x + 20, winScreenRect.y + 40, 300, 250), "Score: " + currentScore.ToString());
          GUI.Label(new Rect(winScreenRect.x + 20, winScreenRect.y + 70, 300, 250), "Level " + Levelnow.ToString() + " Completed");

      }

        if(showPauseScreen)
       {
          Rect pauseScreenRect = new Rect(Screen.width/2 - (Screen.width*.5f/2), Screen.height/2 - (Screen.height*.5f/2), Screen.width*.5f , Screen.height*.5f);
         GUI.Box(pauseScreenRect, "Pause");
           
   
            if(GUI.Button(new Rect(pauseScreenRect.x + pauseScreenRect.width - 170, pauseScreenRect.y + pauseScreenRect.height -60, 150, 45), "Resume"))
            {       
               Time.timeScale = 1f;   
               showPauseScreen = false;

            }

            if(GUI.Button(new Rect(pauseScreenRect.x + 20, pauseScreenRect.y + pauseScreenRect.height -60, 100, 45), "Quit Without Save"))
            {
               Application.LoadLevel("Main Menu");
               Time.timeScale = 1f;
               

            }
        }

        if(showTimeUpScreen)
       {
          Rect pauseScreenRect = new Rect(Screen.width/2 - (Screen.width*.5f/2), Screen.height/2 - (Screen.height*.5f/2), Screen.width*.5f , Screen.height*.5f);
         GUI.Box(pauseScreenRect, "Time's Up!!");
           
   
            if(GUI.Button(new Rect(pauseScreenRect.x + pauseScreenRect.width - 170, pauseScreenRect.y + pauseScreenRect.height -60, 150, 45), "Restart"))
            {       
               Time.timeScale = 1f; 
               showTimeUpScreen = false;
               Application.LoadLevel(Application.loadedLevel);
           
            }

            if(GUI.Button(new Rect(pauseScreenRect.x + 20, pauseScreenRect.y + pauseScreenRect.height -60, 100, 45), "Quit Without Save"))
            {
               Application.LoadLevel("Main Menu");
               Time.timeScale = 1f;
               

            }
        }




   }


   public  void AddToken()
   {
      tokenCount += 1;
      
    }

    void PlaySound(int clip)
   {
      audio.clip = audioClip[clip];
      audio.Play();
   }


}



As usual, if you don't know what my problem is, just simply download my file to take a look at it ( I suck at explaining stuffs, sorry Sad ) : http://www.mediafire.com/view/6qdxp323dpm6gfm/go_home_cube.zip


Oh yeah, if you guys have any tips for my scripting (in C# only) , please voice it out, I would appreciate your help provided.

privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply