Array Question Length (C#)

Hi!

Right, so my problem is, I define an array with the length of 6. But if I print or log the array, it gives this output:

0

0

0

0

0

0

0

6

0

0

0

0

0

0

6

if the array length = 0 it should start a new scene. But I can't do that because 7 out of 8 times it is defined as 0.

I really don't know why!

Here's the code:

public GameObject[] gos;

void Start () {
    /*objPlayer = (GameObject) GameObject.FindWithTag ("Player");
    objCamera = (GameObject) GameObject.FindWithTag ("MainCamera");*/
    gos = GameObject.FindGameObjectsWithTag(tagName);
    /*if (gameObject.tag == "Player") { thisIsPlayer = true; }
    ptrScriptVariable = (VariableScript)objPlayer.GetComponent(typeof(VariableScript) );
    AIscript ptrScriptAI = (AIscript) objPlayer.GetComponent( typeof(AIscript) );*/
    checkEnemys(tagName);
}

private void checkEnemys(string tagName)
{   
    print("Count: "+gos.Length); //HERE IT IS 6!
foreach (GameObject go in gos) {
            newround = false;
            print(go.name);
        }

        Debug.Log(gos.Length); // HERE IT IS 0?!
        //if (gos.Length == 0) {
            //print("No more enemy's");
        //}

}

What the hell is going on??!

I had a thought ( and I could be wrong I don't mess with Unity3d to much)

Your running a check during the Start() function if I am not mistaken start and awake are usually used for initialization.

if your checking every frame shouldn't you be checking in Update()??

I would think there would be a good number of frames before and after start.

Anyhow just a thought.

edit: Are you absolutely sure the game objects are already created before you run this script?

edit 2: This was my test with results.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{
    GameObject[] gos;
    private string myTestTag = "enemy";
    // Use this for initialization
    void Start () 
    {
        gos = GameObject.FindGameObjectsWithTag(myTestTag);
        Debug.Log("Array length Before: " + gos.Length);

        checkTest();
    }

    private void checkTest()
    {
        foreach(GameObject go in gos)
        {
            Debug.Log("Object name: " + go.name);
        }

        Debug.Log("Array Length after: " + gos.Length);
    }

    // Update is called once per frame
    void Update () 
    {

    }
}

To test this I just stuck 6 spheres on the stage and tagged then "enemy" it just seems like the objects are not created yet when your script runs.

It is hard to know what is going on in your scene from the information you have given here, since we cant know how many objects that are in your scene with the tag 'tagName'.

For all I know that is your problem.

Also, when you say that you define an array with the length of 6, do you mean in the inspector? Because that would immediatly be override with

gos = GameObject.FindGameObjectsWithTag(tagName);

uhm yeah srry..

Well I got 6 enemy's in my scene and all of them have the 'Enemy' tag.

the var tagName is defined as Enemy.

So the length of 6 is correct.

And by define I mean:

gos = GameObject.FindGameObjectsWithTag(tagName);

it doesn't override.

But why does it return to 0?

Try debugging it further. Are you sure your tagName is exactly what you expect?

print("tagName: [" + tagName + "].");

Make sure it have same casing, no extra spaces and such (That's why I enclose the tagName in brackets). So it should read in the output.

tagName: [Enemy].

You also have some code that you haven't shown and it's the declaration of newround. Is it just a boolean var or a property? If it is a property, maybe it does something strange with your gos array.

well i know what you mean... the problem is, why does it count back to zero 7 times?

I suddenly realized that the Enemy's have the same script attached (one main scipt for both player and enemy) but the function is private, so it shouldn't be a problem. Still, if it is attached to the enemy, i guess there could be a confilct, but i can't think of any.

but start and update... it doesn't matter. It still doesn't work.

EDIT:

ow I misread.. but it still doesn't solve anything. If I print the array length even when destroying some enemy's while playing it just gives:

00000006000000050000000400000003 etcetera.

output:

tagName: [Enemy]

newround = bool

so everything should be correct... But if I remove everything out of the 'foreach' statement, the problem still occurs..

EDIT:

Even if I remove the whole foreach statement it still gives this output. There are some parts of the script I didn't post, but they have nothing to do with array's or these variables. It's just for sprite animation and such, nothing special.

nope

it was in update in the first place, but it kept logging 00000006000000600000006 so i just put it in the start.

the checkEnemys() function can also placed in the update function but it doesn't solve the problem.

SOLVED! *Thanks to Feltope*.

I just put this part of the script into a new script and only put it on the player prefab and it worked.

Thanks everyone for helping!