null reference exception/array problem

I have the script

var health = 1.0;
var hitsound : AudioClip;
var number = 10.0;

function ApplyDamage (damage : float) {
	health -= damage;
	
	SendMessage("CheckHealth");
}

function CheckHealth () {
	
	if (health <= 0) {
		Destroy (gameObject);
		if (hitsound)
			AudioSource.PlayClipAtPoint(hitsound, transform.position);	
	}
}

function OnGUI () {
	GUI.Box (Rect (10,number,110,30), "Enemy health" + Mathf.Round(health));
}

which is an enemy health script, and

public var pause = false;
var player : Transform;
var Player : Transform;
var fullscreen : boolean;
var Pauseskin : GUISkin;
var allObjects : Array;

function Awake () {
	allObjects = FindObjectsOfType(GameObject);
	
	fullscreen = Screen.fullScreen;
	
	if (player == null  GameObject.FindWithTag("Player"))
	player = GameObject.FindWithTag("Player").transform;
	
	if (Player == null  GameObject.FindWithTag("MainCamera"))
	Player = GameObject.FindWithTag("MainCamera").transform;
}

function Update () {
	if (Input.GetButtonDown("Pause")) {
		if (pause == false) {
			pause = true;
		}
	}
	
	if (pause == false) {
		SendMessage("Unpause");
	}
	
	else if (pause == true) {
		SendMessage("Pause");
	}
	
	if (fullscreen == false) {
		Screen.fullScreen = false;
	}
	
	else if (fullscreen == true) {
		Screen.fullScreen = true;
	}
}

function OnGUI () {
	if (pause == true) {
		GUI.skin = Pauseskin;
		
		GUI.Box (Rect (-5,-5,Screen.width + 10,Screen.height + 10), "Pause");
		
		if (GUI.Button (Rect(Screen.width / 2, Screen.height / 2 + 30,75,50),"Exit")) {
			Application.Quit();
		}
		
		if (GUI.Button (Rect(Screen.width / 2, Screen.height / 2 - 30,75,50),"Resume")) {
			pause = false;
		}
		
		GUI.BeginGroup (Rect (Screen.width - 205, 5, 200, 300));
		
		GUI.Box (Rect (0,0,200,300), "Display Settings");
		fullscreen = GUI.Toggle (Rect (15,25,150,20), fullscreen, "  Full Screen");
		
		
		GUI.EndGroup ();
	}
}

function Pause () {
	for (var go : GameObject in allObjects) {
		if(go.GetComponent(MouseLook)){
			go.GetComponent(MouseLook).enabled = false;
		}
	}
	
	Time.timeScale = 0;
	Screen.lockCursor = false;
}

function Unpause () {
	
	for (var go : GameObject in allObjects) {
		if(go.GetComponent(MouseLook)){
			go.GetComponent(MouseLook).enabled = true;
		}
	}
	
	Time.timeScale = 1;
	Screen.lockCursor = true;
}

which is a game manager script. the enemy belongs to the array in the game manager script, and when i kill the enemy, it is destroyed, but it gives me an error, because the enemy was a part of the array. how can i solve this by removing the enemy from the array, or is there a way i could add objects only containing mouse look scripts? the function that the script mainly covers is pause menu, and pausing. and that is why i need to disable that script.

Can anyone help me?

If you only want to have in your gameObjects array only the gameObjects with mouseLook attached you can add some additional lines to mouseLook and gameManager scripts:
MouseLook:

var id : int;
function Awake(){
   id = gameManager.AddToList(this);
}

GameManager:

static function AddToList(script : MouseLook){
  mouseLookArray.Add(script);
  return mouseLookArray.length;
}

And when your enemy is destroyed get id variable from mouseLook script and do GameManager.mouseLookArray.RemoveAt(id);

Remember, your mouseLookArray variable must be static

no, the enemies dont have the mouse look scripts, I want only the gameObjects with the script attached to them. I dont want the enemies in the array.

Sorry then, my code would work for you anyway, but you wont have to remove scripts from array…

And you will have to replace this:

function Pause () {
	for (var go : GameObject in allObjects) {
		if(go.GetComponent(MouseLook)){
			go.GetComponent(MouseLook).enabled = false;
		}
	}
	
	Time.timeScale = 0;
	Screen.lockCursor = false;
}

function Unpause () {
	
	for (var go : GameObject in allObjects) {
		if(go.GetComponent(MouseLook)){
			go.GetComponent(MouseLook).enabled = true;
		}
	}
	
	Time.timeScale = 1;
	Screen.lockCursor = true;
}

To this:

function Pause () {
	for (var go : MouseLook in mouseLookArray) {
			go.enabled = false;
	}
	
	Time.timeScale = 0;
	Screen.lockCursor = false;
}

function Unpause () {
	
	for (var go : MouseLook in mouseLookArray) {
			go.enabled = true;
	}
	
	Time.timeScale = 1;
	Screen.lockCursor = true;
}

can you make the mouselook code cs based? I code in JavaScript, but that is a script i didnt write.

I’m not very good at C# also, but this may work:

int id = 0;
void Start(){
   id = gameManager.AddToList(this);
}

461781–16211–$MouseLook.cs (2.04 KB)

and what is gameManager referring to? is it referring to the script? because its giving me the error

(I changed the gameManager to GameManager to try to make it work.

I assume, that GameManager is your game manager script

yes, and that is the exact name, but it is still giving me the error

its GameManager.js

You can try placing GameManager.js script to Standart Assets

that doesnt work, is there a way i could do a find command or something?

Try this script, don’t forget to drag object with GameManager script onto its position in mouseLook script

461793–16213–$MouseLook.cs (2.07 KB)

it gives me the errors

and

Redownload the script, I have written it in UnityScript style :smile: I don’t test my scripts before writing…

now im getting the error

I don’t know whats happening… I will try to rewrite mouseLook script in javascript tommorow(it’s 8PM here), maybe c# and UnityScript scripts can’t communicate properly.

thats what i was kind of thinking

its 8pm, i never remember am pm stuff, I go by 24 hour time, even though everyone else i know goes by am pm system. so it that after noon?