Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arnout Swint · Mar 21, 2011 at 09:52 AM · gameobjectarraytagfindgameobjectswithtag

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??!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

8 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 23, 2011 at 10:10 AM 0
Share

Then you don't make an answer, and check your own answer as the correct answer. Check Feltopes answer as correct.

avatar image Arnout Swint · Mar 24, 2011 at 11:10 AM 0
Share

ow whoops srry :P my bad

avatar image
-1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 21, 2011 at 12:00 PM 0
Share

@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.

avatar image
-1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Feltope · Mar 21, 2011 at 11:58 AM 0
Share

interesting that the length would come out like that.

avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

HELP Find gameObject With tag in another array 1 Answer

Need a Custom array Index to look up gameobject tags? 2 Answers

GameObject.findGameObjectsWithTag returning empty? 1 Answer

How to call Class specific methods from an object stored in a GameObject[]? 1 Answer

setting the tag of all gameObjects in an array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges