GUI.Button delays after I click it

I create a button on the screen with GUI.Button and then I use it to create an object. I click it, but then I can’t click it again until the object fades away. The coconuts vanish after so many seconds. Why can I only have one instance of this object at a time?

function OnGUI ()
{if (GUI.Button (Rect (10,10,150,100), "I am a button")){
Coconut = Instantiate(myCoconutPrefab, transform.position, transform.rotation);

How do I make things appear as quickly as I click the button?

There must be something else causing this trouble in your project as that code works just fine for me. I hit the button as quickly as I want and see loads o’ stuff getting instantiated.

Is there more going on? Try it in a bare bones test project (just the code above and a prefab).

Here is all of the code, plus some of the error messages in the log.

//create an exposed variable to which you may drag a rigidbody game object in the inspector
var myCoconutPrefab : Rigidbody;
//the amount of force to use in the launching of the coconut, exposed and therefore adjustable in the inspector
var throwForce : int = 20;
//the audio file variable used when throwing coconuts, stored in this variable for selection in the inspector
var throwClip : AudioClip;
//a static (globally accesible by other scripts) variable set to true/false by the Coconut Switch script
var hitPoint: RaycastHit;

function CC(){
//Create a variable, and in it, store the creation of the rigidbody assigned to myCoconutPrefab, creating it at the position and rotation of the object this script is attached to (the empty game object, ‘thrower’)
var Coconut : Rigidbody = Instantiate(myCoconutPrefab, transform.position, transform.rotation);
//set a velocity for that created rigidbody, in the direction of the Z axis, with the amount assigned to variable ‘throwForce’
Coconut.velocity = transform.TransformDirection(Vector3 (0,0,throwForce));
//ignore collisions between the created object, and the player’s collider (transform.root.collider)

Physics.IgnoreCollision(Coconut.collider, transform.root.collider);
//play the sound file assigned to throwClip just once.
audio.PlayOneShot(throwClip);
}

function OnGUI ()
{ if (GUI.Button (Rect (10,10,150,100), “I am a button”)) {
Coconut = Instantiate(myCoconutPrefab, transform.position, transform.rotation);
//set a velocity for that created rigidbody, in the direction of the Z axis, with the amount assigned to variable ‘throwForce’
Coconut.velocity = transform.TransformDirection(Vector3 (0,0,throwForce));
//ignore collisions between the created object, and the player’s collider (transform.root.collider)
Physics.IgnoreCollision(Coconut.collider, transform.root.collider);
//play the sound file assigned to throwClip just once.
audio.PlayOneShot(throwClip);
}
}

function Update()
{
if(Input.GetButtonDown(“Fire2”))
{CC();
}

OnGUI();

}

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUI.Button (Rect position, System.String text) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:202)
CoconutThrow.OnGUI () (at Assets\My Scripts\CoconutThrow.js:32)
CoconutThrow.Update () (at Assets\My Scripts\CoconutThrow.js:52)

(Filename: Assets/My Scripts/CoconutThrow.js Line: 32)

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUIUtility.cs:379)
UnityEngine.GUI.Button (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:208)
UnityEngine.GUI.Button (Rect position, System.String text) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:202)
CoconutThrow.OnGUI () (at Assets\My Scripts\CoconutThrow.js:32)
CoconutThrow.Update () (at Assets\My Scripts\CoconutThrow.js:52)

(Filename: Assets/My Scripts/CoconutThrow.js Line: 32)

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUIUtility.cs:379)
UnityEngine.GUI.Button (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:208)
UnityEngine.GUI.Button (Rect position, System.String text) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:202)
CoconutThrow.OnGUI () (at Assets\My Scripts\CoconutThrow.js:32)
CoconutThrow.Update () (at Assets\My Scripts\CoconutThrow.js:52)

(Filename: Assets/My Scripts/CoconutThrow.js Line: 32)

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUIUtility.cs:379)
UnityEngine.GUI.Button (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:208)
UnityEngine.GUI.Button (Rect position, System.String text) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:202)
CoconutThrow.OnGUI () (at Assets\My Scripts\CoconutThrow.js:32)
CoconutThrow.Update () (at Assets\My Scripts\CoconutThrow.js:52)

(Filename: Assets/My Scripts/CoconutThrow.js Line: 32)

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUIUtility.cs:379)
UnityEngine.GUI.Button (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:208)
UnityEngine.GUI.Button (Rect position, System.String text) (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUI.cs:202)
CoconutThrow.OnGUI () (at Assets\My Scripts\CoconutThrow.js:32)
CoconutThrow.Update () (at Assets\My Scripts\CoconutThrow.js:52)

(Filename: Assets/My Scripts/CoconutThrow.js Line: 32)

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () (at C:\builds\unity-branches-2.5.x\unity-2.5.x\Runtime\Export\Generated\GUIUtility.cs:379)
UnityEngine.GUI.Button (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style)

Why are you calling OnGUI() from your Update() loop? OnGUI is called by the system. No need to call it.

Also, where is the start of your CC() function? You have a closing } but no starting { before the OnGUI function…

This code is a mess… wanna clean it up and re-post your question with complete code?

That is my entire code.

I removed the OnGUI(); in the update, and it still runs the same. I thought perhaps if I had it constantly updating, it’d work.

I’m using the wonderful example from the Learn me silly website. http://www.learnmesilly.com/index.php?currentPage=unity_lesson5#

Downloaded example 6 from the website. http://www.willgoldstone.com/downloads/unity/

That’s the Coconut Throw script.

Its called by the object thrower which is a child of the main camera object.

Try commenting out the whole of the Update function to see if the OnGUI code works.

I have the same problem with OnGUI…
when i click on the screen (on a random spot), OnGUI delays to ‘listen’… and the strange thing is that when i put a counter inside the OnGUI (adding to it +=1), it gets bigger by 2 times than one (?)