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 El Maxo · Jan 16, 2014 at 11:30 AM · move an objecttransform.

Ai Prefab Spawning in Without a target?

Hi everyone, i have a code that basically makes the AI wonder around, my problem is that when my AI spawning system spawns them in, all there targets disappear, it wouldn't be a issue if i just have to find the player (I have already done this) but i have like 100 way points.

 #pragma strict
 
 var spawnPoints : Transform[];
 var yieldTimeMin = 2;
 var yieldTimeMax = 5;
 
 function Start()
 {
     // create a temporary to store gameObjects by finding with tag
     var spts : GameObject[] = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
 
     // set the size of the spawnpoints array
     spawnPoints = new Transform[ spts.Length ];
 
     // store a reference to each transform from the gameObject array
     for ( var i : int = 0; i < spts.Length; i ++ )
     {
        spawnPoints[i] = spts[i].transform;
     }
 
     // start moving
     Move();
 }
 
 function Move()
 {
     yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
     var target : Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
     GetComponent(NavMeshAgent).destination = target.position;
     yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
     Move();
 }
Comment
Add comment · Show 27
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 El Maxo · Jan 16, 2014 at 12:39 PM 0
Share

can anyone help, its kind of urgent

avatar image Calum-McManus · Jan 16, 2014 at 01:20 PM 1
Share

Ah ok, i may see your issue, in unity, you can't assign objects(or Transforms) to a prefab from the editor, so in this case, if you have added the point to "spawnPoints" in the editor then saved the prefab, the list will be empty.

Try having a central script with a list holding all of these points, then call these into your AI script after the the AI object is in the game!

avatar image AlucardJay · Jan 16, 2014 at 01:20 PM 1
Share

Yes you can call Start.

The problem is when you instantiate the NPC, the array spawnPoints is not populated?

In that case you'll have to populate that array each time you instantiate a NPC. IF you have set a tag and assigned it to all spawn points, you can use GameObject.FindGameObjectsWithTag :

 var spawnPoints : Transform[];
 var yieldTime$$anonymous$$in = 2;
 var yieldTime$$anonymous$$ax = 5;
 
 function Start()
 {
     spawnPoints = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
     
     $$anonymous$$ove();
 }
 
 function $$anonymous$$ove()
 {
     yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));
     var target : Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
     GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
     yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));
     $$anonymous$$ove();
 }


avatar image Calum-McManus · Jan 16, 2014 at 01:24 PM 1
Share

Ok yeah, the best option for you would be to change the variable from a list of Trasnforms to a list of GameObjects! then just you "gameObject.transfrom.position"

avatar image AlucardJay · Jan 16, 2014 at 02:10 PM 1
Share

Example using a timer :

 #pragma strict
 
 public var spawnPoints : GameObject[];
 public var yieldTime$$anonymous$$in : float = 2;
 public var yieldTime$$anonymous$$ax : float = 5;
 
 private var timer : float = 2;
 
 function Start() 
 {
     // reset timer
     timer = Random.Range( yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax );
     
     // store gameObjects by finding with tag
     spawnPoints = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
 }
 
 function Update() 
 {
     // decrease timer
     timer -= Time.deltaTime;
     
     if ( timer < 0 )
     {
         // reset timer
         timer = Random.Range( yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax );
         
         // find a target spawnpoint
         var target : Transform = spawnPoints[ Random.Range(0, spawnPoints.Length) ].transform;
         
         // set the navmesh waypoint
         GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
         Debug.Log( target.position );
     }
 }
Show more comments

1 Reply

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

Answer by AlucardJay · Jan 16, 2014 at 02:18 PM

See the comments for the full story!

The problem is when you instantiate the NPC, the array spawnPoints is not populated? If you have set a tag and assigned it to all spawn points, you can use GameObject.FindGameObjectsWithTag

When I started coding and asking questions, I really found it annoying when the answer given was "really you shouldn't be doing it like this", but .... really you shouldn't be doing it like this. You should use a timer, Invoke or a coroutine.

Here's an example using a timer :

 #pragma strict
 
 public var spawnPoints : GameObject[];
 public var yieldTimeMin : float = 2;
 public var yieldTimeMax : float = 5;
 
 private var timer : float = 2;
 
 function Start() 
 {
     // reset timer
     timer = Random.Range( yieldTimeMin, yieldTimeMax );
     
     // store gameObjects by finding with tag
     spawnPoints = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
 }
 
 function Update() 
 {
     // decrease timer
     timer -= Time.deltaTime;
     
     if ( timer < 0 )
     {
         // reset timer
         timer = Random.Range( yieldTimeMin, yieldTimeMax );
         
         // find a target spawnpoint
         var target : Transform = spawnPoints[ Random.Range(0, spawnPoints.Length) ].transform;
         
         // set the navmesh waypoint
         GetComponent(NavMeshAgent).destination = target.position;
         Debug.Log( target.position );
     }
 }


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 El Maxo · Jan 16, 2014 at 02:23 PM 0
Share

Thanks once again, also thanks to Callum, you guys have been great help

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

22 People are following this question.

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

Related Questions

My character isn't moving 0 Answers

2D Spawn with fixed random locations 0 Answers

How to move a Transform on top of a series of Transforms 0 Answers

Stop a enemy by pointing a light at it. 4 Answers

What would the code look like if I want to spawn cubes of different color every spawn? 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