public var pause = false;
var player : Transform;
var Player : Transform;
var fullscreen : boolean;
var Pauseskin : GUISkin;
var allObjects : GameObject[];
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;
}
and
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);
Debug.Log("IT SHOULD BE WORKING!!!");
if (hitsound)
AudioSource.PlayClipAtPoint(hitsound, transform.position);
}
}
function OnGUI () {
GUI.Box (Rect (10,number,110,30), "Enemy health" + Mathf.Round(health));
}
and I get en error saying that there is a null reference exception because there is an array in the first script, and the gameobject connected to the second script is destroyed, and i need to remove that object from the array, i would like to do this with tags (It seems to make sense to do it this way) but I would like to know what is best.
I mainly use c# and to get that collection I would do the following in c#
GameObject[] bodies = FindObjectsOfType(typeof(GameObject)) as GameObject[];
That would give me a collection of all game objects, now when I want to do something with them:
foreach (GameObject body in bodies)
{
if(body.tag.Contains("value"))
{
// do something here
}
}
From your loop, it looks like you are trying to walk all of the game objects you have collected, but from your error, you have not collected any game objects, I guess it is just hard to figure out where your real problem is other than if your array is null, it did not find any game objects which makes no sense.
no, it finds all of the objects in the scene, but when the enemy health script destroys the enemy, it gives me an error saying that the array is null because that object isnt there, and it can access it, but it has the location it was in last, the scripts work fine, i just want to exclude the enemy from the array so the error doesnt occur.
Oh, well, the array is not dynamic as far as I know, so you will have to do some swapping, unless you use generics or lists even a hash table. If you do not use a dynamic array, you will have to create a second array, fill it with the active enemies, null the first array and reset the first = to the second then null the second, a lot of overhead, but it would accomplish what you want.
Look in the docs for Array, find ScriptReference/Array.html, use that example to build your array of game objects, then you can remove items and add items without having to dance.
Change your loop over the objects to reflect the following -
var allObjects : Array;
function Start() {
allObjects = FindObjectsOfType(GameObject);
}
// ... later:
for ( var ix : int = 0; ix < allObjects.length; ix++ ) {
if ( !allObjects[ix] ) {
allObjects.RemoveAt(ix);
ix--;
} else {
// do your stuf - check tags or health or what have you
}
}
That’ll remove null objects from the list as they’re encountered. You don’t need to remove them, that’s just an added bonus. THe null exception is because you’re trying to access part of something that doesn’t exist, so check to see it exists before you try to access part of it.
Also, look at how Vicenti is using Array vs [ ], one is dynamic, one is static (aka: Array is dynamic, and [ ] is static), so what he is doing is walking your collection of game objects, finding the one you want to get rid of, removing it from the array at the index position (which shortens the array by 1) changes the loop counter point by subtracting one since he just got rid of that item in the index so the index state has changed. This is why I mentioned to read the docs on an Array and how it works so you can get a solid understanding of Array’s in Unity.
you were getting answers, just not the one you were looking for, and Vicenti gave you a golden egg instead of expecting you to do some leg work on your own and look at collections, lists, and how Array works, you have your answer and it is in Vicenti’s post, you didn’t like it so you posted a duplicate thread? pointless