Scripting help needed

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

#pragma strict


var fullWidth : float = 256;      //set the maximum width of the bar to be used for the maths function below

var dartcount : float = 3;

var increasing : boolean = false; //create a boolean flag we can use to stop and start the addition to power

var shooting : boolean = false;  //create a boolean flag we can use to stop the player shooting during the shot

var barSpeed : float = 25;       //speed to increment the bar

var ball : Rigidbody;//create a slot to assign my cannonball prefab to.

var blastPart : ParticleEmitter;//create a blast particle slot to assign particle emitter to

var cannonLight : Light;//create a light slot to assign a light prefab for when the blast occurs

var spawnPos : Transform;//create a slot to assign an empty game object as the point to spawn from

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

var endgame : boolean = false;

var cannonBlast : AudioClip; //audio for the blast

private var thePower : float;  //create a private variable (not shown in inspector) to store the current set power


function Start(){
//set the power bar to zero at start
// guiTexture.pixelInset.width = 0;
guiTexture.pixelInset = Rect (10,20,0,20);
}
function Update () {
  //if we are not currently shooting and Jump key is pressed down
  if(!shooting && Input.GetButtonDown("Fire1"))
  { if (dartcount > 0) {
   //play the sound set on the audio source
   audio.Play();
   //set the increasing part of Update() below to start adding power
   increasing=true;
   endgame = false;
   }
  }
  // detect if Jump key is released and then call the Shoot function, passing current
  // value of 'thePower' variable into its 'power' argument
  if(!shooting && Input.GetButtonUp("Fire1")){
   //reset increasing to stop charge of the power bar
   increasing = false;
   //call the custom function below with current value of thePower fed to its argument
   Shoot(thePower);
  }
  if(increasing){
   //add to thePower variable using Time.deltaTime multiplied by barSpeed
   thePower += Time.deltaTime * barSpeed;
   //stop (or 'fight') thePower from exceeding fullWidth using Clamp
   thePower = Mathf.Clamp(thePower, 0, fullWidth);
   //set the width of the GUI Texture equal to that power value
   guiTexture.pixelInset.width = thePower;
   //set the pitch of the audio tone to the power var but step it down with division
   audio.pitch = thePower/30;
  }
}
// start the 'Shoot' custom function, and establish a
// float argument to recieve 'thePower' when function is called
function Shoot(power : float){
//stop shooting occuring whilst currently shooting with this boolean flag
shooting  = true;
//create a particle burst
var pBlast : ParticleEmitter = Instantiate(blastPart, spawnPos.position, spawnPos.rotation);
//base blast amount on power argument, and divide it to diminish power
pBlast.maxEmission = power/4;
//create a light to act as a flash for the blast, base its range & intensity
//upon the power variable and destroy it after 0.1 seconds
var canLight : Light = Instantiate(cannonLight, spawnPos.position, spawnPos.rotation);
canLight.intensity = power/7;
canLight.range=power/7;
Destroy(canLight.gameObject, 0.1);
//stop the audio source on this object to cut off the tone build up to launch
audio.Stop();
//play the sound of the cannon blast in a new object to avoid interfering
//with the current sound assignment and loop setup
AudioSource.PlayClipAtPoint(cannonBlast, transform.position);
//create a ball, assign the newly created ball to a var called pFab
var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);
dartcount -= 1;
//find the forward direction of the object assigned to the spawnPos variable
var fwd : Vector3 = spawnPos.forward;
pFab.AddForce(fwd * power * shotForce);
//Destroy(pFab.gameObject, 4);
//pause before resetting everything
yield WaitForSeconds(0);
//reset the bar GUI width and our main power variable
guiTexture.pixelInset.width = 0;
thePower = 0;
//allow shooting to occur again
shooting = false;
if (dartcount == 0)
{
     endgame = true;
}
}

function OnGUI ()
{
    if(endgame)
    {
    GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 300));
       // RenderSettings.fogDensity = 1;
     GUI.Box (Rect (0,0,100,200), "GameOver");
    
        if (GUI.Button (Rect(10,40,80,30), "Quit")){
       
        Application.LoadLevel("menu");
      }
     if  (GUI.Button (Rect(10,80,80,30), "Restart")){
        
            Application.LoadLevel("test");
      }
           GUI.EndGroup ();  
    }
}

any help would be great and thank you in advance

bump

I find your source code almost impossible to read. The error message says:

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.

I don’t know if GUI “functions” rules include the other aspects, like:

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.

i managed to solve part 2 of the issue but the power is still not showing up at the min

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

#pragma strict

var score : float = 0;



function Update ()
{
    var f1score = GetComponent ( "f1Score" );
   
}

function AddScore (points : int)
{
    score = points += score ;
}

the sender

var pointsvalue    : int = 1;

private var points : int;

var playerGameObject;


function Start ()
{
    playerGameObject = GameObject.FindWithTag ( "DartBoard" );
}


function  OnTriggerEnter( other : Collider)

{
        if(other.transform.name == "Dart")
        {
            points+=pointsvalue;
            AddtoScore();
        }
}


function AddtoScore ()
{
   
    var playerConnect = playerGameObject.GetComponent ( DartBoardPoints );
    playerConnect.points += pointsvalue;
}

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

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.

that would be good but i’m still using unity 4.5 version of unity at the minute

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

Bump*

im looking into converting the script above for the new UI system and ima wondering if im going in the right direction

#pragma strict
import UnityEngine.UI;

//var fullWidth : float = 256;      //set the maximum width of the bar to be used for the maths function below

var maxValue: float = 256;

var Power : Slider;

var dartcount : float = 3;

var increasing : boolean = false; //create a boolean flag we can use to stop and start the addition to power

var shooting : boolean = false;  //create a boolean flag we can use to stop the player shooting during the shot

var barSpeed : float = 25;       //speed to increment the bar

var ball : Rigidbody;//create a slot to assign my cannonball prefab to.

var blastPart : ParticleEmitter;//create a blast particle slot to assign particle emitter to

var cannonLight : Light;//create a light slot to assign a light prefab for when the blast occurs

var spawnPos : Transform;//create a slot to assign an empty game object as the point to spawn from

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

var endgame : boolean = false;

var cannonBlast : AudioClip; //audio for the blast

private var thePower : float;  //create a private variable (not shown in inspector) to store the current set power
private var minValue : float;


function Start(){
//set the power bar to zero at start
   // guiTexture.pixelInset.width = 0;
    //guiTexture.pixelInset.height = 20;
    Power.minValue = 0;

//guiTexture.pixelInset = Rect (10,20,0,0);
}
function Update () {
  //if we are not currently shooting and Jump key is pressed down
  if(!shooting && Input.GetButtonDown("Fire1"))
  { if (dartcount > 0) {
   //play the sound set on the audio source
   audio.Play();
   //set the increasing part of Update() below to start adding power
   increasing=true;
   endgame = false;
   }
  }
  // detect if Jump key is released and then call the Shoot function, passing current
  // value of 'thePower' variable into its 'power' argument
  if(!shooting && Input.GetButtonUp("Fire1")){
   //reset increasing to stop charge of the power bar
   increasing = false;
   //call the custom function below with current value of thePower fed to its argument
   Shoot(thePower);
  }
  if(increasing){
   //add to thePower variable using Time.deltaTime multiplied by barSpeed
   thePower += Time.deltaTime * barSpeed;
   //stop (or 'fight') thePower from exceeding fullWidth using Clamp
   thePower = Mathf.Clamp(thePower, 0, maxValue);
   //set the width of the GUI Texture equal to that power value
   //guiTexture.pixelInset.width = thePower;
   Power.minValue = thePower;
   //set the pitch of the audio tone to the power var but step it down with division
   audio.pitch = thePower/30;
  }
}
// start the 'Shoot' custom function, and establish a
// float argument to recieve 'thePower' when function is called
function Shoot(power : float){
//stop shooting occuring whilst currently shooting with this boolean flag
shooting  = true;
//create a particle burst
var pBlast : ParticleEmitter = Instantiate(blastPart, spawnPos.position, spawnPos.rotation);
//base blast amount on power argument, and divide it to diminish power
pBlast.maxEmission = power/4;
//create a light to act as a flash for the blast, base its range & intensity
//upon the power variable and destroy it after 0.1 seconds
var canLight : Light = Instantiate(cannonLight, spawnPos.position, spawnPos.rotation);
canLight.intensity = power/7;
canLight.range=power/7;
Destroy(canLight.gameObject, 0.1);
//stop the audio source on this object to cut off the tone build up to launch
audio.Stop();
//play the sound of the cannon blast in a new object to avoid interfering
//with the current sound assignment and loop setup
AudioSource.PlayClipAtPoint(cannonBlast, transform.position);
//create a ball, assign the newly created ball to a var called pFab
var pFab : Rigidbody = Instantiate(ball, spawnPos.position, spawnPos.rotation);
dartcount -= 1;
//find the forward direction of the object assigned to the spawnPos variable
var fwd : Vector3 = spawnPos.forward;
pFab.AddForce(fwd * power * shotForce);
//Destroy(pFab.gameObject, 4);
//pause before resetting everything
yield WaitForSeconds(0);
//reset the bar GUI width and our main power variable
//guiTexture.pixelInset.width = 0;
Power.minValue = 0;
thePower = 0;
//allow shooting to occur again
shooting = false;
if (dartcount == 0)
{
      yield WaitForSeconds(4);
      endgame = true;
      Time.timeScale = 0.0;
}
}

function OnGUI ()
{
  
 
    if(endgame)
    {
  
   
    GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 300));
       // RenderSettings.fogDensity = 1;
     GUI.Box (Rect (0,0,100,200), "GameOver");
   
        if (GUI.Button (Rect(10,40,80,30), "Quit")){
         Time.timeScale = 1.0;
        Application.LoadLevel("menu");
       
      }
     if  (GUI.Button (Rect(10,80,80,30), "Restart")){
       
         Time.timeScale = 1.0;
            Application.LoadLevel("test");
           
      }
           GUI.EndGroup (); 
    }
}

Don’t worry about the Ongui part as im still editing the script at this time