Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 cidmodder · Jul 12, 2011 at 02:38 AM · javascriptarrayai

Building an array help

I'm trying to build an array of targets for the ai to attack. There are three teams A B and C and I'm trying to get team C to attack either B or A at random by picking them from an array list. I can build an array for C to attack either team A or B but bot both. Heres what I have so far:

 var possibleTargets = new Array();
 function Start()
 {
      possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanA"));
      possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanB"));
       target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
     }
 
 function Update () {
 
 if (target==null){
 target= possibleTargets[Random.Range(0, possibleTargets.length)].transform;
 }

with this way i don't get any errors but it also doesn't pick a target. If it picks a target everything else in the script works fine. Hope somebody can point me in the right direction! Thanks!

EDIT:

I tried this function and it didn't work either. target is still coming back null and the possibleTargets is coming back as just a list of arrays.

var possibleTargets = new Array();

function Start() { var tempTargets = GameObject.FindGameObjectsWithTag("HumanA"); for(gameObject in tempTargets) { possibleTargets.Add(tempTargets); print(possibleTargets.length); }

 tempTargets = GameObject.FindGameObjectsWithTag("HumanB");
 for(gameObject in tempTargets)
 {
 possibleTargets.Add(tempTargets);
 }
 target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
 print(target);
 }
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
0
Best Answer

Answer by Waz · Jul 14, 2011 at 11:02 PM

Here's an easier approach:

 var possibleTargets : GameObject[];
 
 function Start()
 {
      possibleTargets = GameObject.FindGameObjectsWithTag("HumanA")
         + GameObject.FindGameObjectsWithTag("HumanB");
      target = possibleTargets[Random.Range(0, possibleTargets.Length)].transform;
 }
 
 function Update ()
 {
     if (!target)
         target= possibleTargets[Random.Range(0, possibleTargets.Length)].transform;
 }


Two tricks here: array addition (JavaScript only, don't try in C#), and using the Boolean operator of GameObject to know when it has been destroyed (comparing with null is not the same thing).

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
0

Answer by digitalConundrum · Jul 12, 2011 at 04:26 AM

The best advice I can think of is to add a few print or debug.log statements (those are c#, I'm not sure what the equivalent statements are in javascript) which display possibleTargets.length after each attempt to add a new gameObject to the array, just to make sure that the array is actually being built properly.

Though the issue is (I think, I could be incorrect and I'm sure someone will correct me if that's the case)that you're making target a transform type instead of a GameObject. If my understanding of that is correct, you're only getting the transform of the initial target, and only the transform that it was in when the target was assigned. This being said, your variable target will NEVER be null because destroying the current target will not change the value of the transform variable you have stored.

My suggestion is to try changing your target variable into a GameObject and then call target.transform whenever you need to work with the current location of the target(ie in your pathfinding/seeker script.) This will also cause target==null when the target is no longer in existance.

Sorry for the long winded answer with no concrete code example, but unless my understanding of the way a transform variable is treated, you should have better success if you try this out.

EDIT:

instead of

 var possibleTargets = new Array();
 function Start()
 {
      possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanA"));
      possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanB"));
       target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
     }

try something like

 var possibleTargets = new Array();
 function Start()
 {
    var tempTargets=GameObject.FindGameObjectsWithTag("HumanA");
 
    foreach(gameObject temp in tempTargets)
            {
                possibleTargets.add(temp);
            }
 
    tempTargets=GameObject.FindGameObjectsWithTag("HumanB");
            {
                possibleTargets.add(temp);
            }

}

I'll admit that I really have no idea what the syntax would be for js, so i used the c# foreach statement, but I'm sure something similar exists in js.Before you were essentially creating an array of arrays if my understanding of your comment is corrent.

Hope this helps!

Comment
Add comment · Show 6 · 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 cidmodder · Jul 13, 2011 at 01:04 AM 0
Share

what you're saying does make some since and after debugging for a bit I found out that the possibletargets only keeps track of how many arrays it has not how many game objects there are. (i.e. I have two A $$anonymous$$m and one B $$anonymous$$m and it will say that possibletargets.length = 2 not 3) any suggestions from here?

avatar image Eric5h5 · Jul 13, 2011 at 02:34 AM 0
Share

print and Debug.Log aren't C#, they're Unity functions (print is an alias of Debug.Log).

avatar image digitalConundrum · Jul 13, 2011 at 04:38 AM 0
Share

@Eric5h5... fair enough on the print and Debug.log, I wasnt sure if their implementation would differ from language to language....

@cidmodder okay, I think I see what your problem may be...try to do something like I'm about to add in an edit to my answer.

avatar image cidmodder · Jul 13, 2011 at 09:51 PM 0
Share

@digitalConundrum I tried a few different variations on what you gave me and kept getting the same problem. I added what I had changed in the edit. From what I could find for is the same as foreach. what is the temp in tempobjects for? I couldn't get that to work no matter how i did it.

avatar image digitalConundrum · Jul 13, 2011 at 10:13 PM 0
Share

GameObject temp was a temporary variable that would be assigned to each item in the array as the foreach statement passed over each....sending this from my phone, will update with better info when I am at my pc.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Noonb array question - Boolean makes a script activate for all elements in an array. Problem is the only element that gets the script activated is the first one. 1 Answer

More Efficient way? 1 Answer

array of boxes 2 Answers

Simple follow script issue 1 Answer

Private Array in custom Class behaving Public -1 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