- Home /
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??!
Answer by Feltope · Mar 21, 2011 at 11:52 AM
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.
Answer by Arnout Swint · Mar 23, 2011 at 10:00 AM
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!
Then you don't make an answer, and check your own answer as the correct answer. Check Feltopes answer as correct.
Answer by Arnout Swint · Mar 21, 2011 at 11:10 AM
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.
@Arnout Swint: please use comments for discussion ins$$anonymous$$d of placing answers. The answers should be kept answers for a reason and the same goes with comments.
Answer by Arnout Swint · Mar 21, 2011 at 11:56 AM
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.
Answer by Skjalg · Mar 21, 2011 at 09:58 AM
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);