Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 15, 2014 at 09:20 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jammer3000 · Dec 10, 2014 at 05:20 PM · javascriptvariablescore

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");
     }
 }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jammer3000 · Dec 11, 2014 at 01:47 PM 0
Share

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.");
     }
 }
avatar image lolzrofl · Dec 15, 2014 at 07:15 PM 0
Share

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.

avatar image meat5000 ♦ · Dec 15, 2014 at 07:18 PM 1
Share

...Destroys a game on Android. The audio delay I mean.

avatar image Jammer3000 · Dec 15, 2014 at 07:39 PM 0
Share

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.

avatar image Jammer3000 · Dec 15, 2014 at 09:07 PM 1
Share

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

Show more comments

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity3d Javascript Reference Script Variable 1 Answer

gameObject.active Find 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Change variable value of a script from another script. both in different Game object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges