Problem with array on gameobjects

Hello Everyone,

I’m having some trouble on getting my script working and some help would be really appreciated. I’ve created an array and placed some game objects (petals) in it.
I’m trying to do that every time I press the right mouse button then, the code looks for a gameobject within the array and add a rigidbody to it which causes the object to fall.

The sound does work, so the error has to be related to petals.GameObject.rigidbody = petCharge[charge];

I’m familiar with using arrays to replace texture, but never tried it on gameobjects before. Thanks in advance!

The Error: Nullreferenceexception object reference not set to an instance of an object

The Code:

// Custom

static var charge : int = 0;
var petalsound : AudioClip;

// The Array

var petCharge : GameObject[];
var petals : GameObject;

function Start(){

	charge = 0;
}

function Update(){

	 //Custom
	if (Input.GetButton("Fire2")){
	
	SendMessage ("Petalchange");
	
	}
}

// Button

function Petalchange () {

	if(charge >= petCharge.Length -1) // Setting the limit to the charge to be -1 as length
           return;
    
	audio.PlayOneShot(petalsound);
	charge++;
	petals.gameObject.rigidbody = petCharge[charge]; // I get the error here
    print ("works");
}

I’m still not sure if i got it right. This is what i got: You have an array of gameobjects. Every time you press down the right mouse button you want one / the next object in this array to drop (attach a rigidbody).

There are some things wrong:

  • GetButton will return true as long as you hold the button down. So you will call Petalchange every frame while the button is down. Use “GetButtonDown” instead.
  • You shouldn’t use Sendmessage when you call another function in the same class. Just invoke the function like this: Petalchange();
  • You do a range check, but you check before you increment the variable. So when you reach the limit you can still get out of bounds. Do the check either after you changed the variable or change the variable after you used it.
  • Array indices start at “0” since you increment your variable before you use it you will start with “1” and effectively skip the first one.
  • Like DanielKim said the rigidbody property is read only. It can only return a value.
  • Also components can’t be created and later be attached to a gameobject. Components can’t exist without a GameObject they’re attached to. You have to use AddComponent to attach a component to a gameobject.
  • Even when the assignment to the rigidbody property would work, you try to assign a GameObject to it. That doesn’t make much sense.

Here’s an example how to to what you’ve described:

private var current : int = 0;
var petalsound : AudioClip;
var petCharge : GameObject[];

function Update()
{
    if (Input.GetButtonDown("Fire2"))
    {
        Petalchange();
    }
}

function Petalchange()
{
    if(charge > petCharge.Length -1)
        return;

    audio.PlayOneShot(petalsound);

    petCharge[charge].AddComponent.<Rigidbody>();
    charge++;
}

edit
If you want to drop all objects at once, just use a loop like this:

// ...
function Petalchange()
{
    audio.PlayOneShot(petalsound);
    for (var i = 0; i < petCharge.Length; i++)
        petCharge*.AddComponent.<Rigidbody>();*

}

Well then, rigidbody is read only, lol.

What you can do too is set your petal prefab to have a rigidbody by default, and set it to isKinematic by default, then activate it through code.

petal.rigidbody.isKinematic = false;

Or, you need to AddComponent for the rigidbody, which would be: