Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripting help needed

Discussion in 'Scripting' started by oldcollins, Nov 13, 2014.

  1. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    I've got this power bar script which i'm having issues with which are

    1. the power bar itself is not showing up and the screen

    2. i keep getting this error

    ArgumentException: You can only call GUI functions from inside OnGUI.
    UnityEngine.GUIUtility.CheckOnGUI () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUIUtility.cs:229)
    UnityEngine.GUI.BeginGroup (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:1085)
    UnityEngine.GUI.BeginGroup (Rect position) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:1075)
    Shooting.EndGame () (at Assets/Shooting.js:125)
    Shooting+$Shoot$5+$.MoveNext () (at Assets/Shooting.js:118)


    here is the code

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4. var fullWidth : float = 256;      //set the maximum width of the bar to be used for the maths function below
    5.  
    6. var dartcount : float = 3;
    7.  
    8. var increasing : boolean = false; //create a boolean flag we can use to stop and start the addition to power
    9.  
    10. var shooting : boolean = false;  //create a boolean flag we can use to stop the player shooting during the shot
    11.  
    12. var barSpeed : float = 25;       //speed to increment the bar
    13.  
    14. var ball : Rigidbody;//create a slot to assign my cannonball prefab to.
    15.  
    16. var blastPart : ParticleEmitter;//create a blast particle slot to assign particle emitter to
    17.  
    18. var cannonLight : Light;//create a light slot to assign a light prefab for when the blast occurs
    19.  
    20. var spawnPos : Transform;//create a slot to assign an empty game object as the point to spawn from
    21.  
    22. var shotForce : float = 5;// create a number to multiply the force by as the value of up to 256 may not be enough to effectively shoot a ball forward
    23.  
    24. var endgame : boolean = false;
    25.  
    26. var cannonBlast : AudioClip; //audio for the blast
    27.  
    28. private var thePower : float;  //create a private variable (not shown in inspector) to store the current set power
    29.  
    30.  
    31. function Start(){
    32. //set the power bar to zero at start
    33. // guiTexture.pixelInset.width = 0;
    34. guiTexture.pixelInset = Rect (10,20,0,20);
    35. }
    36. function Update () {
    37.   //if we are not currently shooting and Jump key is pressed down
    38.   if(!shooting && Input.GetButtonDown("Fire1"))
    39.   { if (dartcount > 0) {
    40.    //play the sound set on the audio source
    41.    audio.Play();
    42.    //set the increasing part of Update() below to start adding power
    43.    increasing=true;
    44.    endgame = false;
    45.    }
    46.   }
    47.   // detect if Jump key is released and then call the Shoot function, passing current
    48.   // value of 'thePower' variable into its 'power' argument
    49.   if(!shooting && Input.GetButtonUp("Fire1")){
    50.    //reset increasing to stop charge of the power bar
    51.    increasing = false;
    52.    //call the custom function below with current value of thePower fed to its argument
    53.    Shoot(thePower);
    54.   }
    55.   if(increasing){
    56.    //add to thePower variable using Time.deltaTime multiplied by barSpeed
    57.    thePower += Time.deltaTime * barSpeed;
    58.    //stop (or 'fight') thePower from exceeding fullWidth using Clamp
    59.    thePower = Mathf.Clamp(thePower, 0, fullWidth);
    60.    //set the width of the GUI Texture equal to that power value
    61.    guiTexture.pixelInset.width = thePower;
    62.    //set the pitch of the audio tone to the power var but step it down with division
    63.    audio.pitch = thePower/30;
    64.   }
    65. }
    66. // start the 'Shoot' custom function, and establish a
    67. // float argument to recieve 'thePower' when function is called
    68. function Shoot(power : float){
    69. //stop shooting occuring whilst currently shooting with this boolean flag
    70. shooting  = true;
    71. //create a particle burst
    72. var pBlast : ParticleEmitter = Instantiate(blastPart, spawnPos.position, spawnPos.rotation);
    73. //base blast amount on power argument, and divide it to diminish power
    74. pBlast.maxEmission = power/4;
    75. //create a light to act as a flash for the blast, base its range & intensity
    76. //upon the power variable and destroy it after 0.1 seconds
    77. var canLight : Light = Instantiate(cannonLight, spawnPos.position, spawnPos.rotation);
    78. canLight.intensity = power/7;
    79. canLight.range=power/7;
    80. Destroy(canLight.gameObject, 0.1);
    81. //stop the audio source on this object to cut off the tone build up to launch
    82. audio.Stop();
    83. //play the sound of the cannon blast in a new object to avoid interfering
    84. //with the current sound assignment and loop setup
    85. AudioSource.PlayClipAtPoint(cannonBlast, transform.position);
    86. //create a ball, assign the newly created ball to a var called pFab
    87. var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);
    88. dartcount -= 1;
    89. //find the forward direction of the object assigned to the spawnPos variable
    90. var fwd : Vector3 = spawnPos.forward;
    91. pFab.AddForce(fwd * power * shotForce);
    92. //Destroy(pFab.gameObject, 4);
    93. //pause before resetting everything
    94. yield WaitForSeconds(0);
    95. //reset the bar GUI width and our main power variable
    96. guiTexture.pixelInset.width = 0;
    97. thePower = 0;
    98. //allow shooting to occur again
    99. shooting = false;
    100. if (dartcount == 0)
    101. {
    102.      endgame = true;
    103. }
    104. }
    105.  
    106. function OnGUI ()
    107. {
    108.     if(endgame)
    109.     {
    110.     GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 300));
    111.        // RenderSettings.fogDensity = 1;
    112.      GUI.Box (Rect (0,0,100,200), "GameOver");
    113.    
    114.         if (GUI.Button (Rect(10,40,80,30), "Quit")){
    115.        
    116.         Application.LoadLevel("menu");
    117.       }
    118.      if  (GUI.Button (Rect(10,80,80,30), "Restart")){
    119.        
    120.             Application.LoadLevel("test");
    121.       }
    122.            GUI.EndGroup ();  
    123.     }
    124. }

    any help would be great and thank you in advance
     
  2. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I find your source code almost impossible to read. The error message says:

    Code (csharp):
    1. Shooting.EndGame () (at Assets/Shooting.js:125)
    which means that you have a function in your script called EndGame(), and that's around line 125. The code you've included in your question doesn't have a line 125, so it's hard to relate the problem with the code.

    The error message basically says that you are calling GUI functions from outside of OnGUI(). I can't see where you do that in the code you've included.
     
  4. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    I don't know if GUI "functions" rules include the other aspects, like:
    Code (CSharp):
    1. guiTexture.pixelInset.width = thePower;
    In any case, the error is pretty detailed, it tells you that this specific line (Shooting+$Shoot$5+$.MoveNext () (at Assets/Shooting.js:118) is trying to call GUI functions, and is telling you that it is not allowed outside of OnGui().

    If you need to change what's on the GUI anyway, you could (should?) use private entries in that class to store those things that need changing, and apply it on your next run of the OnGui() call.
     
  5. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i managed to solve part 2 of the issue but the power is still not showing up at the min
     
  6. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    need some help again i have these two scripts and im just wondering if im going the right way or not here are the scripts

    the receiver

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var score : float = 0;
    4.  
    5.  
    6.  
    7. function Update ()
    8. {
    9.     var f1score = GetComponent ( "f1Score" );
    10.    
    11. }
    12.  
    13. function AddScore (points : int)
    14. {
    15.     score = points += score ;
    16. }
    17.  
    the sender
    Code (JavaScript):
    1. var pointsvalue    : int = 1;
    2.  
    3. private var points : int;
    4.  
    5. var playerGameObject;
    6.  
    7.  
    8. function Start ()
    9. {
    10.     playerGameObject = GameObject.FindWithTag ( "DartBoard" );
    11. }
    12.  
    13.  
    14. function  OnTriggerEnter( other : Collider)
    15.  
    16. {
    17.         if(other.transform.name == "Dart")
    18.         {
    19.             points+=pointsvalue;
    20.             AddtoScore();
    21.         }
    22. }
    23.  
    24.  
    25. function AddtoScore ()
    26. {
    27.    
    28.     var playerConnect = playerGameObject.GetComponent ( DartBoardPoints );
    29.     playerConnect.points += pointsvalue;
    30. }
    31.  
    as i said above just wondering if this is the right kind of way to go if not nay help would be grateful
    thanks in advance
     
  7. forbidden

    forbidden

    Joined:
    Mar 6, 2013
    Posts:
    36
    I hate to say it but if your project is still new I would ditch the old gui stuff and start learning the new 4.6 stuff.

    I found the new unity gui system much better.
    And when I try to use old gui stuff like onguu it don't even work most of the time.

    I think the old gui stuff is going to be depreciated.
     
  8. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    that would be good but i'm still using unity 4.5 version of unity at the minute
     
  9. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    the main issue at the min are the two scripts that are not communicating with each other ask for the power bar is just not showing up at the min but i'm thinking of converting it to an NGUI
     
  10. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
  11. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    im looking into converting the script above for the new UI system and ima wondering if im going in the right direction

    Code (JavaScript):
    1.  
    2. #pragma strict
    3. import UnityEngine.UI;
    4.  
    5. //var fullWidth : float = 256;      //set the maximum width of the bar to be used for the maths function below
    6.  
    7. var maxValue: float = 256;
    8.  
    9. var Power : Slider;
    10.  
    11. var dartcount : float = 3;
    12.  
    13. var increasing : boolean = false; //create a boolean flag we can use to stop and start the addition to power
    14.  
    15. var shooting : boolean = false;  //create a boolean flag we can use to stop the player shooting during the shot
    16.  
    17. var barSpeed : float = 25;       //speed to increment the bar
    18.  
    19. var ball : Rigidbody;//create a slot to assign my cannonball prefab to.
    20.  
    21. var blastPart : ParticleEmitter;//create a blast particle slot to assign particle emitter to
    22.  
    23. var cannonLight : Light;//create a light slot to assign a light prefab for when the blast occurs
    24.  
    25. var spawnPos : Transform;//create a slot to assign an empty game object as the point to spawn from
    26.  
    27. var shotForce : float = 5;// create a number to multiply the force by as the value of up to 256 may not be enough to effectively shoot a ball forward
    28.  
    29. var endgame : boolean = false;
    30.  
    31. var cannonBlast : AudioClip; //audio for the blast
    32.  
    33. private var thePower : float;  //create a private variable (not shown in inspector) to store the current set power
    34. private var minValue : float;
    35.  
    36.  
    37. function Start(){
    38. //set the power bar to zero at start
    39.    // guiTexture.pixelInset.width = 0;
    40.     //guiTexture.pixelInset.height = 20;
    41.     Power.minValue = 0;
    42.  
    43. //guiTexture.pixelInset = Rect (10,20,0,0);
    44. }
    45. function Update () {
    46.   //if we are not currently shooting and Jump key is pressed down
    47.   if(!shooting && Input.GetButtonDown("Fire1"))
    48.   { if (dartcount > 0) {
    49.    //play the sound set on the audio source
    50.    audio.Play();
    51.    //set the increasing part of Update() below to start adding power
    52.    increasing=true;
    53.    endgame = false;
    54.    }
    55.   }
    56.   // detect if Jump key is released and then call the Shoot function, passing current
    57.   // value of 'thePower' variable into its 'power' argument
    58.   if(!shooting && Input.GetButtonUp("Fire1")){
    59.    //reset increasing to stop charge of the power bar
    60.    increasing = false;
    61.    //call the custom function below with current value of thePower fed to its argument
    62.    Shoot(thePower);
    63.   }
    64.   if(increasing){
    65.    //add to thePower variable using Time.deltaTime multiplied by barSpeed
    66.    thePower += Time.deltaTime * barSpeed;
    67.    //stop (or 'fight') thePower from exceeding fullWidth using Clamp
    68.    thePower = Mathf.Clamp(thePower, 0, maxValue);
    69.    //set the width of the GUI Texture equal to that power value
    70.    //guiTexture.pixelInset.width = thePower;
    71.    Power.minValue = thePower;
    72.    //set the pitch of the audio tone to the power var but step it down with division
    73.    audio.pitch = thePower/30;
    74.   }
    75. }
    76. // start the 'Shoot' custom function, and establish a
    77. // float argument to recieve 'thePower' when function is called
    78. function Shoot(power : float){
    79. //stop shooting occuring whilst currently shooting with this boolean flag
    80. shooting  = true;
    81. //create a particle burst
    82. var pBlast : ParticleEmitter = Instantiate(blastPart, spawnPos.position, spawnPos.rotation);
    83. //base blast amount on power argument, and divide it to diminish power
    84. pBlast.maxEmission = power/4;
    85. //create a light to act as a flash for the blast, base its range & intensity
    86. //upon the power variable and destroy it after 0.1 seconds
    87. var canLight : Light = Instantiate(cannonLight, spawnPos.position, spawnPos.rotation);
    88. canLight.intensity = power/7;
    89. canLight.range=power/7;
    90. Destroy(canLight.gameObject, 0.1);
    91. //stop the audio source on this object to cut off the tone build up to launch
    92. audio.Stop();
    93. //play the sound of the cannon blast in a new object to avoid interfering
    94. //with the current sound assignment and loop setup
    95. AudioSource.PlayClipAtPoint(cannonBlast, transform.position);
    96. //create a ball, assign the newly created ball to a var called pFab
    97. var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);
    98. dartcount -= 1;
    99. //find the forward direction of the object assigned to the spawnPos variable
    100. var fwd : Vector3 = spawnPos.forward;
    101. pFab.AddForce(fwd * power * shotForce);
    102. //Destroy(pFab.gameObject, 4);
    103. //pause before resetting everything
    104. yield WaitForSeconds(0);
    105. //reset the bar GUI width and our main power variable
    106. //guiTexture.pixelInset.width = 0;
    107. Power.minValue = 0;
    108. thePower = 0;
    109. //allow shooting to occur again
    110. shooting = false;
    111. if (dartcount == 0)
    112. {
    113.       yield WaitForSeconds(4);
    114.       endgame = true;
    115.       Time.timeScale = 0.0;
    116. }
    117. }
    118.  
    119. function OnGUI ()
    120. {
    121.  
    122.  
    123.     if(endgame)
    124.     {
    125.  
    126.    
    127.     GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 300));
    128.        // RenderSettings.fogDensity = 1;
    129.      GUI.Box (Rect (0,0,100,200), "GameOver");
    130.    
    131.         if (GUI.Button (Rect(10,40,80,30), "Quit")){
    132.          Time.timeScale = 1.0;
    133.         Application.LoadLevel("menu");
    134.        
    135.       }
    136.      if  (GUI.Button (Rect(10,80,80,30), "Restart")){
    137.        
    138.          Time.timeScale = 1.0;
    139.             Application.LoadLevel("test");
    140.            
    141.       }
    142.            GUI.EndGroup ();
    143.     }
    144. }
    Don't worry about the Ongui part as im still editing the script at this time