Search Unity

True or False Bug?

Discussion in 'Scripting' started by pauloaguiar, Jul 18, 2013.

  1. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    I don´t know if is a bug or not, here is the problem:

    I have a CarStats_Playerscript and player damage script.

    the problem is when is showing the "warning!" text (GUI)
    when the player as 20% of health call CarStats_Player.showWarning = true;

    In the scene editor works fine, show the text gui "Warning!"
    But after "BUILD" and run is player as 20% of health the gui text "warning!" dont show??
    Whats is the problem? static var showWarning : boolean = false; can not be call from other script???
    See picture:
     

    Attached Files:

    Last edited: Jul 18, 2013
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    We can't help if you don't show us the code.
     
  3. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Here is the code:

    CarStats_Player:
    Code (csharp):
    1.  
    2.  
    3. private var activeWayPoint : WayPoint;
    4. //Strings text.
    5. var subMenuLoad : String = "MainMenu";
    6. //Lap counter.
    7. var laps : int = 0;
    8. //Max of laps.
    9. var lapsMax : int = 3;
    10. var finalLap : int = 2;
    11. //sounds.
    12. var VoiceSoundGoal : AudioClip;
    13. var VoiceSoundLastLap : AudioClip;
    14. // Gui Styles.
    15. var LapGUIStyle : GUIStyle;
    16. var FinalFlagGUI : GUIStyle;
    17. var WarninglFlagGUI : GUIStyle;
    18. // GUi vectors.
    19. var WarningXeYPos = Vector2(10,600);
    20.  
    21.  
    22.  
    23.  
    24. var XeYPos = Vector2(5,35);
    25. var XeYPos2 = Vector2(5,65);
    26. var LapScoreXYPos = Vector2(5,95);
    27. var FlagPos = Vector2(5,350);
    28. // Booleans.
    29. private var showFlagWindow : boolean = false;
    30. private var showLastlapInfo : boolean = false;
    31. static var showWarning : boolean = false;
    32.  
    33.  
    34.  
    35.  
    36. // Strings, levels and number.
    37. var levelName : String = "LEVEL2";
    38. var levelNumber : int = 2;
    39.  
    40.  
    41.  
    42.  
    43. function Start()
    44. {
    45.     //activeWayPoint = WayPoint.start.next;
    46.     activeWayPoint = WayPoint.start;
    47. }
    48.  
    49.  
    50. //var dist : float = 10;
    51.  
    52.  
    53.  
    54.  
    55. // Keeps track of when the player reaches the goal
    56. function OnTriggerEnter (triggerWaypoint : Collider)
    57. {
    58.     // We allow the player to go through the waypoints only one after the other so he can't skip waypoints.
    59.     if (activeWayPoint.collider == triggerWaypoint)
    60.     {
    61.         // When we reach the game might be finished!
    62.         if (activeWayPoint == WayPoint.start)
    63.         {
    64.             //count laps.
    65.             laps++;
    66.            
    67.             //If laps is the same as lapsMax, then do....
    68.             if (laps == lapsMax)
    69.             {                                
    70.                 Savedata();
    71.                 ReachedGoal();                                                                                                                                                                                                                                                                
    72.             }            
    73.             if (laps == finalLap)
    74.             {
    75.                 AudioSource.PlayClipAtPoint(VoiceSoundLastLap, transform.position);
    76.                 showLastlapInfo = true;
    77.                 turnOfLastlapInfo();
    78.             }
    79.            
    80.             //if (Vector3.Distance(transform.position, transform.position) < dist)
    81.             //{
    82.                 //Debug.Log("Check Point!");
    83.             //}            
    84.         }
    85.                                        
    86.         activeWayPoint = activeWayPoint.next;            
    87.     }    
    88. }
    89. // Save data
    90. function Savedata()
    91. {
    92.     // add here your playerPrefs.SetInt
    93.     //Example:        
    94.    
    95.     //PlayerPrefs.SetInt("points",ColPoints.points);
    96.     //PlayerPrefs.SetInt("kills",killCounter.curkills);
    97.     //PlayerPrefs.SetInt(levelName, levelNumber);    
    98. }
    99. // Load level or main menus.
    100. function LoadingLevel()
    101. {
    102.     yield WaitForSeconds(5);
    103.     Application.LoadLevel(subMenuLoad);
    104. }
    105.  
    106.  
    107. // I have reach the number of lap then stop
    108. function ReachedGoal()
    109. {
    110.     // Stop driving if an AI car has completed the race!
    111.     var aicar : AI_NOVO = GetComponent(AI_NOVO);
    112.     if (aicar != null)
    113.         aicar.enabled = false;            
    114.    
    115.     LoadingLevel();    
    116.        
    117.     //enable the 3d text attach to the car
    118.     //TextToShow.renderer.enabled = true;
    119.    
    120.     //check and slow the car regidbody in running and drag, to prevent the car moving alone.
    121.     // show GUi flag.
    122.     rigidbody.drag = 2.5;
    123.     AudioSource.PlayClipAtPoint(VoiceSoundGoal, transform.position);
    124.     showFlagWindow = true;
    125.  
    126.  
    127. }
    128. // turn boolean false if last lap GUI is true.
    129. function turnOfLastlapInfo()
    130. {
    131.     yield WaitForSeconds (5);
    132.     showLastlapInfo = false;
    133. }
    134.  
    135.  
    136. function OnGUI()
    137. {
    138.     //GUI.color = Color.yellow;        
    139.     //GUI.Label(Rect(scorePos.x, scorePos.y,100,100), laps.ToString());
    140.     GUI.Button (new Rect(XeYPos.x, XeYPos.y, 150, 30),"Laps: " + laps, LapGUIStyle);
    141.     GUI.Button (new Rect(XeYPos2.x, XeYPos2.y, 150, 30),"Total: " + lapsMax, LapGUIStyle);
    142.    
    143.     //show  and enabled current boolean.
    144.     // if finish the lap then do.
    145.     if (showFlagWindow == true)
    146.     {
    147.         GUI.Button (new Rect(FlagPos.x, FlagPos.y, 250, 30),"Congratulations!", FinalFlagGUI);
    148.     }
    149.     // if is last lap then do
    150.     if (showLastlapInfo == true)
    151.     {
    152.         GUI.Box (new Rect(FlagPos.x, FlagPos.y, 250, 30),"Last Lap!", FinalFlagGUI);
    153.     }    
    154.    
    155.     if (showWarning == true)
    156.     {
    157.         GUI.Box (new Rect(WarningXeYPos.x, WarningXeYPos.y, 250, 30),"WARNING!!", WarninglFlagGUI);
    158.     }    
    159. }
    160.  
    161.  
    162. @script ExecuteInEditMode()

    PlayerDamage:

    Line 52 , CarStats_Player.showWarning = true;



    Code (csharp):
    1. //in the object Enimigo.-RigidBody//com explosão
    2. var damage = 1;
    3. var health = 100;
    4. var explosion : Transform;
    5. var SmokeR : GameObject;
    6.  
    7.  
    8. var deadReplacement : Transform;
    9. var VoiceEnergylow : AudioClip;
    10. var WallCollisionSound : AudioClip;
    11.  
    12.  
    13. var PlayerHealthGUI : GUIStyle;
    14. var healthPos = Vector2(5,155);
    15.  
    16.  
    17. var CollisionStregth = 1;
    18.  
    19.  
    20. var AlertRed :boolean =false;
    21. var AlertNormal :boolean =true;
    22.  
    23.  
    24. var waterSplash : GameObject;
    25.  
    26.  
    27. function Awake ()
    28. {    
    29.     SmokeR.particleEmitter.enabled = false;  //add particles to the object directly.
    30. }
    31.  
    32.  
    33. function Update ()
    34. {    
    35. }
    36.  
    37.  
    38. function OnCollisionEnter(collision : Collision)
    39. {
    40.     if (collision.relativeVelocity.magnitude > CollisionStregth)
    41.     {
    42.         if ((collision.gameObject.tag == "COM") || (collision.gameObject.tag == "WALL"))
    43.         {
    44.             if (health <= 0) // If Object health is Zero(0) then destroy.
    45.             {            
    46.                 Detonate ();            
    47.                 var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);              
    48.             }
    49.               // check if is low health then play sound and enabled smoke particles.
    50.             else if (health == 20)
    51.             {
    52.                 CarStats_Player.showWarning = true;            
    53.                 AudioSource.PlayClipAtPoint(VoiceEnergylow, transform.position);
    54.                 SmokeR.particleEmitter.enabled = true;
    55.                 AlertRed = true;              
    56.             }                            
    57.             health -= damage;                
    58.         }
    59.        
    60.         // Play wall colision sound
    61.           if (collision.relativeVelocity.magnitude >= 0.1)
    62.            {            
    63.                 AudioSource.PlayClipAtPoint(WallCollisionSound, transform.position);
    64.                                          
    65.         }          
    66.     }        
    67.                            
    68. }
    69.  
    70.  
    71. function OnTriggerEnter(trig : Collider)
    72. {
    73.     if (trig.gameObject.tag == "water")
    74.     {
    75.         Instantiate (waterSplash, transform.position, Quaternion.identity);
    76.     }    
    77. }
    78.  
    79.  
    80. function Detonate ()
    81. {
    82.     // Destroy ourselves        
    83.     Destroy(gameObject);
    84.        
    85.     // Replace ourselves with the dead body
    86.     if (deadReplacement)
    87.     {
    88.         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
    89.        
    90.         // Copy position  rotation from the old hierarchy into the dead replacement
    91.         CopyTransformsRecurse(transform, dead);
    92.     }
    93. }
    94. static function CopyTransformsRecurse (src : Transform,  dst : Transform)
    95. {
    96.     dst.position = src.position;
    97.     dst.rotation = src.rotation;
    98.    
    99.     for (var child : Transform in dst)
    100.     {
    101.         // Match the transform with the same name
    102.         var curSrc = src.Find(child.name);
    103.         if (curSrc)
    104.             CopyTransformsRecurse(curSrc, child);
    105.     }
    106. }
    107.  
    108.  
    109. function OnGUI()
    110. {
    111.     //GUI.color = Color.cyan;
    112.     //GUI.Label(Rect(healthPos.x, healthPos.y,100,100), health.ToString());
    113.     if (AlertRed)
    114.     {
    115.         GUI.color = Color.red;
    116.         GUI.Button (new Rect(healthPos.x, healthPos.y, 150, 30),"HEALTH: " + health, PlayerHealthGUI);    
    117.         AlertNormal = false;      
    118.     }
    119.     if (AlertNormal)
    120.     {
    121.         GUI.color = Color.green;
    122.         GUI.Button (new Rect(healthPos.x, healthPos.y, 150, 30),"HEALTH: " + health, PlayerHealthGUI);
    123.     }
    124. }
    125. @script ExecuteInEditMode()
    126.  
    127.  
    128.  
     
    Last edited: Jul 18, 2013
  4. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    You're trying to access CarStats_Player in line 52 when the name of your script is Car_Stats_Player.
     
  5. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Is a error on the post is CarStats_Player and not Car_Stats_Player.

    problem remains.
     
  6. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    if(health == 20) shouldn't it be if(health<=20){.......}

    You're saying that the warning sign should only be should when health is at 20 but not below 20.
     
  7. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Problem solved.

    The problem is in the inspector script setup , Warning Xe YPos = X >10, Y 600 to 250.
    fixed Y pos to 250. It was 600 show Warning text out of screen.

    Tks all of you.
     

    Attached Files:

    Last edited: Jul 18, 2013