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 Mioperj · Apr 08, 2012 at 07:21 AM · spawnmissingunits

Spawned units disappear

Hello Unity Community, I am new here, but i was searching for a while about why my spawned units just disappear or seems to teleport from the location they r born. Sorry for my english. But hope someone can help me with that issue.

Thanks!

Here is the code i got:

// Add this script to a Parent GameObject of the spawnPoints.

// Note: enemyPrefab will have an AI script attached which will already Tag the Player object

// so it won't be needed here.

var spawnPoints : Transform[]; // Array of spawn points to be used.

var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.

var amountEnemies = 20; // Total number of enemies to spawn.

var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.

var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.

function Start()

{

 Spawn();

}

function Spawn()

{

for (i=0; i

{

   yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.



   var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.

   var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.



   Instantiate(obj, pos.position, pos.rotation); 

}

}

Comment
Add comment · Show 1
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 GuyTidhar · Apr 08, 2012 at 08:37 AM 1
Share

What scripts are there on the prefabs? It could be that one of them is responsible for what you are describing, since I don'y see you are doing anything with the reference to the instantiated enemies (cause if you had, you would assign the instantiated obj to a list of enemies).

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darkcookie · Apr 08, 2012 at 08:52 AM

well thats simple the enemys arent teleporting there just spawning at a random locations so if u have more than one spawn just delete the

 var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];

 

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 Mioperj · Apr 09, 2012 at 05:34 AM

Sorry, i found the problem, but couldnt solve it, its my random movement script, right now i am using another one but not so good as the one i got this problem.

Thanks for the answers!!

Here is the code:

 enum Type2D {XY,XZ,YZ}

var Type2D : Type2D = Type2D.XZ;

var yourMovementObject : Transform;

var randomRate = 1.0;

var moveSpeed = 1.0;

class MaxPosition

{

 var UpMaxPositionX = 5;

 var DownMaxPositionX = -5;



 var UpMaxPositionY = 5;

 var DownMaxPositionY = -5;



 var UpMaxPositionZ = 5;

 var DownMaxPositionZ = -5;

}

var MaxPosition : MaxPosition;

private var RandomX : int;

private var RandomY : int;

private var RandomZ : int;

function Update ()

{

 Invoke("RandomlyMove",randomRate);

//////////////////////////////////////////////x,y,z/////////////////////////////////////////////////////////////////////////////

switch (Type2D)

{

     case Type2D.XY:

         yourMovementObject.Translate(Vector3(RandomX,RandomY,0) * moveSpeed * Time.deltaTime,Space.World);

         break;

     case Type2D.XZ:

         yourMovementObject.Translate(Vector3(RandomX,0,RandomZ) * moveSpeed * Time.deltaTime,Space.World);

         break;

     case Type2D.YZ:

         yourMovementObject.Translate(Vector3(0,RandomY,RandomZ) * moveSpeed * Time.deltaTime,Space.World);

         break;

 }



 if (yourMovementObject.transform.position.x > MaxPosition.UpMaxPositionX)

     yourMovementObject.transform.position.x = MaxPosition.UpMaxPositionX;

 else if (yourMovementObject.transform.position.x < MaxPosition.DownMaxPositionX)

     yourMovementObject.transform.position.x = MaxPosition.DownMaxPositionX;



 if (yourMovementObject.transform.position.y > MaxPosition.UpMaxPositionY)

     yourMovementObject.transform.position.y = MaxPosition.UpMaxPositionY;

 else if (yourMovementObject.transform.position.y < MaxPosition.DownMaxPositionY)

     yourMovementObject.transform.position.y = MaxPosition.DownMaxPositionY;



 if (yourMovementObject.transform.position.z > MaxPosition.UpMaxPositionZ)

     yourMovementObject.transform.position.z = MaxPosition.UpMaxPositionZ;

 else if (yourMovementObject.transform.position.z < MaxPosition.DownMaxPositionZ)

     yourMovementObject.transform.position.z = MaxPosition.DownMaxPositionZ;

}

function RandomlyMove ()

{

 RandomX = Random.Range(-2,3);

 RandomY = Random.Range(-2,3);

 RandomZ = Random.Range(-2,3);

 CancelInvoke("RandomlyMove");

}

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

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

6 People are following this question.

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

Related Questions

Script spawns another script within the gameobject 2 Answers

Spawn System Troubles 0 Answers

Debug.Log error 2 Answers

Script crashing unity. 1 Answer

C# Make array based on script variable 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