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)