- Home /
The question is answered, right answer was accepted
Accessing Script From Other Script Causes Lag?
Hi in the script below I am accessing my Score_Control_Script in order to change a variable inside it, but when I do it this way it cause a lag in my game (I am doing this same thing with the UIControllerScript), not a big one but every time that piece of code runs which is quite often in my game it causes the entire thing to freeze for about a half a second and then unfreeze?
I have used this method many times before and it works just fine so I do not know what the problem is? Thank you for your help in advance (:
var scoreControlScript : Score_Control_Script;
var scoreValue = 1;
var scoreSound : AudioClip;
// Used to disable sound fx when user disables sound fx button
var UIControllerScript : UIController;
function OnTriggerEnter2D (other : Collider2D)
{
switch (other.tag) {
case "ScoreCollider":
if (!gameover) {
scoreControlScript.currentScore += scoreValue;
if (UIControllerScript.muteSoundEffects == false) {
audio.PlayOneShot(scoreSound, 0.3);
}
}
break;
case "Respawn":
Destroy(gameObject);
break;
default:
Debug.Log("Enemy_Controller_Script - OnTriggerEnter2D: switch statement didn't find any tag");
}
}
}
Answer by lolzrofl · Dec 11, 2014 at 12:26 AM
Is currentScore a static variable? If not, you should be accessing it with a reference, not by the class directly. Also, you don't setup your reference to scoreControlScript.
Yes sir, currentScore is a static variable and also you are right I adjusted it so I do setup the reference, unless you mean something else? How the code looks now in the question is how it is in my code in the game. Also below is my Score_Control_Script. What I do not get though is even if I take out the scoreControlScript out and not reference my Score_Control_Script what so ever, the UIControllerScript.muteSoundEffects == false in my if statement also causes the slight lag?
#pragma strict
static var currentScore : int = 0;
static var highScore : int = 0;
var currentScoreGUI : GUIText;
var highScoreGUI : GUIText;
var currentScoreGameoverGUI : GUIText;
var highScoreGameoverGUI : GUIText;
var offsetY : float = 40;
var sizeX : float = 100;
var sizeY : float = 40;
function Awake () {
// $$anonymous$$ake the game run as fast as possible in the web player
Application.targetFrameRate = 300;
}
function Start() {
currentScore = 0;
GetHighScore();
// Debug.Log("High score is " + highScore);
}
// Creat GUI box then call new rect to make a rectangle, set its position, size, and text(in that order)
function OnGUI () {
//GUI.Box (new Rect (Screen.width/2-sizeX/2, offsetY, sizeX, sizeY), "Score: " + currentScore);
currentScoreGUI.text = " " + currentScore;
highScoreGUI.text = " " + highScore;
currentScoreGameoverGUI.text = " " + currentScore;
highScoreGameoverGUI.text = " " + highScore;
// Debug.Log("OnGUI();");
}
function Update() {
// This should be called specifically when the score increments, but this works for now.
if (currentScore > highScore) {
// If the current score is greater than the high score, then set high score to the current score.
// Otherwise, high score would always equal current score.
highScore = currentScore;
}
}
function SetHighScore() {
// Sets our high score as an int in Player Preferences with the tag 'High Score'.
PlayerPrefs.SetInt("High Score", highScore);
// Debug.Log("High score is set to " + highScore);
}
function GetHighScore() {
// Check the Player Preferences for something with the tag 'High Score'.
if(PlayerPrefs.Has$$anonymous$$ey("High Score")){
// High score exists, so we can grab it.
highScore = PlayerPrefs.GetInt("High Score");
// Debug.Log("Grabbed the saved high score of " + highScore);
}else{
// High score doesn't exist, so set it to zero.
// This would happen when the player first plays the game, and hasn't gotten a high score yet.
highScore = 0;
// Debug.Log("High score not found, setting it to zero.");
}
}
It could be the audio.PlayOneShot(scoreSound, 0.3);
that is causing the lag. $$anonymous$$ake sure the audio clip you are using for scoreSound
is set to native format (WAV). If it is compressed, it will cause lag when Unity decompresses the audio.
...Destroys a game on Android. The audio delay I mean.
It is set to the native format (WAV). And I tested it all the way down to that line of code that increments the score is the only thing that causes the lag... I still can't see why. And right now it does not do it in the editor just on my iPhone 5 when I load it from Xcode onto my phone.
It worked!!! :D alls I really needed to do was only update the GameOverGUI.text whenever the game ended and the lag stopped and also @lolzrofl your way worked great too. Thank you so much for your help I really appreciate it :D