unity3d - C# frames running twice? -


this has me utterly confused. using unity3d , c# scripts , seems if code running twice per frame. on button down have sprite change position , changes once @ least think does.

i added debug in , getting results this:

score 1 @ 3.569991 @ frame 168

score 2 @ 3.57414 @ frame 168

score 3 @ 3.818392 @ frame 183

score 4 @ 3.820178 @ frame 183

and forth carrying on. not updating score in other scripts. there more script printing score out on screen.

is there reason why script may run this?

full script:

 using unityengine;  using system.collections;  public class score : monobehaviour {      public static int highscore;     public static int myscore;     public static bool allowscore;     public guitext mytext;      public static bool whichscene;       //only allows score start when first object has passed player object      void ontriggerenter2d(collider2d collisionobject) {          allowscore = true;          debug.log ("allowscore true");      }       void start () {           highscore = playerprefs.getint("highscore");           int scale = screen.height / 20;          mytext.fontsize = scale;      }       //add 1 score every switch      void update () {           // need stop score counting          if (deathonimpact.dead == true) {             allowscore = false;           } if (input.getmousebuttondown (0) && allowscore == true && whichscene == true) { // added spawnerobjectmovement.whichscene == true              //input.getmousebuttondown (0)              //input.getkeydown("space")              myscore = myscore + 1;             debug.log ("my score " + myscore + " point(s)" + " @ time:" + time.realtimesincestartup + " @ frame:" + time.framecount);          } if (myscore > highscore) {             highscore = myscore;             playerprefs.setint("highscore", highscore);          }          mytext.text = myscore.tostring ();         //mytext.text = "score: " + myscore.tostring ();          if (score.whichscene == false) {             int scale = screen.height / 40;             mytext.fontsize = scale;             mytext.text = "practice mode";          }      } } 

the script attached triggerobject, sprite, , gui text

whichscene referes button pressed, 'play' normal play or 'practice mode' easier version. score disabled 'practice mode'.

update: have edited out have added since problem arose , has not been fixed. im going check unity setting see if has changed. seems in build setting old scene deleted around when problem arose not selcted 'shadowed' cant select it. scene exact copy of playscene. sign of problem?

solution: appears separating script 2 smaller scripts has solved issue. still unsure why has arisen since working before one, oh guess. thank you.

based on comments, have said script attached 3 gameobjects, means update() method getting called 3 times per frame.

public class score : monobehaviour {     public static int myscore; 

you have declared myscore static int. functionally means shared instances of score script run.

the update() method of monobehaviour called once per frame every gameobject has script attached. have 3 gameobjects script attached. therefore, each call update() on individual instance of score script.

i'm not sure intend happen, it's hard me give advice beyond pointing out problem.


i think need split script multiple scripts. script doing much. it's violating single responsibility principal (srp). 1 of important principles follow in programming. i'd suggest splitting @ least 3 scripts. i'd make following scripts:

  1. playerstatistics - attach script player object (i'm assuming sprite mentioned). hold statistics, including score, player. there should 1 attached each player.
  2. scoreboard - attach script gui component. take reference playerstatistics. notice reference single instance on player. scoreboard script read value of score playerstatistics script.
  3. scoringtrigger - attach triggerobject. have reference playerstatistics script. have code checks see if scoring should done, , updates value of playerstatistics script.

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -