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
1
Question by Xatoku · Jul 27, 2012 at 12:10 PM · arraytagforprint

For Loop Not Iterating Properly?

This one is strange. I've got a for loop set up to add each gameObject of an array to another array. I have a print check to make sure it's working, so I know the for loop is iterating, but it keeps on adding the same gameObject to the array. It makes absolutely zero sense.

 var newTargets : List.<TargetClass>;
 var nullTarget : TargetClass;

 public class TargetClass{
     var target : GameObject;
     var threat : float;
 }

 function Start(){
     FindTarget();
 }

 function FindTarget(){
     for(var p : GameObject in GameObject.FindGameObjectsWithTag("Friendly")){
         nullTarget.target = p;
         print(nullTarget.target);
         newTargets.Add(nullTarget);
     }
 }

So let's say we have two objects called "Man 01" and "Man 02." If we try to add them to newTargets, it will just add "Man 02" twice, yet it will print "Man 01", and then "Man 02." So the nullTarget says that it's switching, but when I check the editor, it hasn't.

Somebody please help me. This is driving me insane!

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

2 Replies

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

Answer by Bunny83 · Jul 27, 2012 at 12:34 PM

You have to create an instance of your TargetClass for each target. It's a reference type. If you just create one instance, you will be using this one instance all the time and add the same reference to your List.

Btw: nullTarget i badly worded and shouldn't be a member variable.

 public class TargetClass
 {
     var target : GameObject;
     var threat : float;
 }
 
 var newTargets : List.<TargetClass>;
 
 function Start()
 {
     FindTarget();
 }
 
 function FindTarget()
 {
     for(var p : GameObject in GameObject.FindGameObjectsWithTag("Friendly"))
     {
         var item = new TargetClass();
         item.target = p;
         print(item.target);
         newTargets.Add(item);
     }
 }
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 Xatoku · Jul 27, 2012 at 12:38 PM 0
Share

I almost want to meet you just so I can hug you. Thank you so much. Not only did you answer my question, but explained why my process is wrong and taught me something in the process. Few people on here manage to pull that off. Thanks again, Bunny. Cheers!

avatar image Bunny83 · Jul 27, 2012 at 12:43 PM 0
Share

You're welcome! It's not really something special ;) Just some basic OOP.

avatar image
0

Answer by Seth-Bergman · Jul 27, 2012 at 12:22 PM

newTargets.Add(nullTarget.target); }

you're setting the "target" data member of nullTarget, but never changing nullTarget itself, just it's data member "target"

but on the last line you just add nullTarget, should be nullTarget.target I guess

EDIT:

 function FindTarget(){
      for(var p : GameObject in GameObject.FindGameObjectsWithTag("Friendly")){
      nullTarget = p.GetComponent(TargetClass);
      print(nullTarget);
      newTargets.Add(nullTarget);
      }

this way you are passing each object, instead of the same one each time

Comment
Add comment · Show 4 · 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 Xatoku · Jul 27, 2012 at 12:31 PM 0
Share

No because newTargets is a list of TargetClass, so adding just the target of nulltarget wouldn't work.

avatar image Seth-Bergman · Jul 27, 2012 at 12:34 PM 0
Share

then you should be setting p = nullTarget.

you are only changing the nullTarget.target var, but nullTarget is still the same object..

see my edit above

avatar image Bunny83 · Jul 27, 2012 at 12:45 PM 1
Share

TargetClass is a plain data class and not a $$anonymous$$onoBehaviour component. You spotted the problem, but your solution isn't quite right ;)

avatar image Seth-Bergman · Jul 27, 2012 at 12:53 PM 0
Share

Now I see.. my eyes went straight to the bottom .. (still a bit harsh voting me down tho) :}

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question about FindObjectsOfType 1 Answer

is an Array the most efficent way of finding the closest gameobject? 1 Answer

View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers

CompareTag and OverlapSphere 1 Answer

Problem with array 2 Answers


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