GameObjects Array Problem

Hello everyone! First post here, but I already spent much time here looking for answers, and this community is awesome. I hope someday I’ll be skilled enough to help others.

Here is what I’m trying to do:
When I pick an item, it allows me to modify some rigidbodie’s drag parameter, so it slows them if I press a key. It all works fine, except I got this error:
NullReferenceException: Object reference not set to an instance of an object
PlayerDefense.FixedUpdate () (at Assets/2DJumpNRunFramework/Scripts/PlayerDefense.js:57)

But, it works fine when I’m playing in Unity. The problem is when I play with my GS2 on Unity Remote, sometimes it works, sometimes nothing happens when I press the key. And when I compile for Android and test it directly on my GS2, nothin ever happens.

Here is the code for when I press the button:

function SetSlowTime(slowget:float){
slowtime=slowtime+slowget;
}

function FixedUpdate () {

if(FdefenseDown() && slowtime>0){
	print("baboum!");
	slowtime=slowtime-1;		
	var Slowable = GameObject.FindGameObjectsWithTag("slowable" || "bullet");
	if(Slowable!=null){
	for(var g : GameObject in Slowable) {
	g.GetComponent(SlowScript).slow();
		}
	}
}				

}

And here is the code placed on every rigidbody:

function slow () {

 if(rigidbody.drag == 0){
 	rigidbody.drag = 20;
 	rigidbody.angularDrag = 10;
 	yield WaitForSeconds (5);
 	rigidbody.drag = 0;
 	rigidbody.angularDrag = 0;}
 else{
 	rigidbody.drag = 0;
 	rigidbody.angularDrag = 0;}
 }

Does anyone knows about an issue with modifying rigidbodies properties on mobile devices?..

Thanks in advance.
Julien

There are a couple of things I can see in your code that isn’t very mobile friendly,

Use #pragma strict in the top of your script if you don’t have it already.

You should connect deltaTime to any time parameter to be independent on all devices. Or you could make use of InvokeReapeating to save a few CPU-cycles.

Try to work around finding objects in realtime, instead do it at Start() or Awake() and fetch them into an array. Then you can use something like:

private var slowArray : Rigidbody[] = new Rigidbody[10];
for (var i : int = 0; i<slowArray.length; i++) {
    slowArray*.drag = 20;*

slowArray*.angularDrag = 10;*
//and so on
}
For-loops are extremely fast!
Don’t forget to reuse objects instead of creating new ones.

Thanks a lot for the advices, I’ll apply that.
About my problem itself, the error came from a misplaced tag on an object wich didn’t have the “slow” script.

About the bug, it came from the “else” part of my slow script. By getting 2 items, I allowed 2 access to the script. So the first time it would slow the rigidbodies, then on the second press inside the 5sec timelapse, it would deactivate it.
It worked well on computer, but on mobile device, the touch system often get 2 or 3 taps instead of 1. That’s why the slow order got cancelled as soon as it was launched.
I added a delay between the taps orders and everything is fine now.

Anyway, thanks a lot Save, for your precious advices. It will help the game going faster, but have a lot of scripts to rewrite that way now! ^^

Julien