Android Build help needed regarding ball throw

I am using this script which is running perfectly alright and the ball is thrown forward properly.

var ballPrefab : Transform;
function OnGUI() {
var e : Event = Event.current;
if(e.button == 0 && e.isMouse){
//Debug.Log(“Left Click”);
if(actionStatus == false)
if(Input.GetButtonDown(“Fire1”)) {
var ball = Instantiate(ballPrefab, transform.position, Quaternion.identity);
ball.rigidbody.AddForce(transform.forward * 900);
actionStatus = true;

I changed platform to Android to build and run. The scrip shows compiler error:

BCE0019: ‘rigidbody’ is not a member of ‘UnityEngine.Object’
Error building because scripts had compiler errors
Exception: Error building Player because scripts had compiler errors
UnityEditor.BuildPlayerWindow.BuildPlayerWithDefaultSettings (Boolean askForBuildLocation, BuildOptions forceOptions) (at C:…)

I changed the script to:

var ballPrefab : GameObject;
if(Input.GetButtonDown(“Fire1”)) {
var ball: GameObject = Instantiate(ballPrefab, transform.position, Quaternion.identity) as GameObject;
ball.rigidbody.AddForce(transform.forward * 900);

Now, there is another compiler error:
NullReferenceException: Object reference not set to an instance of an object

and ‘rigidbody’ error is cleared.

Kindly help me resolve this error.


Zia Khatri

Just had to remove the script from the ball prefab in camera and re-set.

It worked well.