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 Mz3D · Jul 30, 2012 at 12:20 PM · gameobjectinstantiatearray

Different array value at different times

I have a GameObject array with a number of different objects. I want - at different times - to instantiate the object stored in GameObject[0], then the one stored in GameObject[1] and so on.. i've created this script but it doesn't work..

 var enemies : GameObject[];
 
 function Update () {
     if ((Time.time > 0) && (Time.time < 5)){
         enemies = enemies[0];  // i think the problem is in this line
         GenerateEnemy ();
     }
      if ((Time.time > 5) && (Time.time < 10)){
         enemies = enemies[1];
         GenerateEnemy ();
     }
 }


 function GenerateEnemy (){
     Instantiate(enemies, Vector3(0,0,0), transform.rotation);
 }


Anyone knows where i'm wrong?

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

1 Reply

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

Answer by AlucardJay · Jul 30, 2012 at 12:29 PM

You are trying to store different things in the same place. Try :

EDIT :

 var enemies : GameObject[];
 var currentEnemy : GameObject;
 var spawnTimer : float = 0.0;
 
 function Update () {
     if ((Time.time % 10.0 > 0) && (Time.time % 10.0 < 5)){
         currentEnemy = enemies[0];
     }
     if ((Time.time % 10.0 > 5) && (Time.time % 10.0 < 10)){
         currentEnemy = enemies[1];
     }
 
     spawnTimer += Time.deltaTime;
     if (spawnTimer > 1.0){ // enemy is spawned every 1.0 seconds
         GenerateEnemy();
         spawnTimer = 0.0;
     }
 }
 
 
 function GenerateEnemy (){
     Instantiate(currentEnemy , Vector3(0,0,0), transform.rotation);
 }




(with original script) Note that this will Instantiate an enemy (type 0 or type 1) every frame.

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 Mz3D · Jul 30, 2012 at 12:32 PM 0
Share

Yes, in fact i noticed this problem, how could i solve it?

avatar image AlucardJay · Jul 30, 2012 at 01:06 PM 0
Share

add a counter/timer. then when the timer is reached, spawn and reset the timer.

I have edited the answer that now takes into consideration spawning the enemy once every 1 second, and also continues to spawn different types of enemy after 10 seconds.

avatar image Mz3D · Jul 30, 2012 at 01:19 PM 0
Share

how does a timer work? where can i read about it?

avatar image AlucardJay · Jul 30, 2012 at 01:24 PM 0
Share

I have edited the answer. A timer is just a var you set yourself. See my answer :

 var spawnTimer : float = 0.0;

This is just a variable to hold the value for reference. Then in Update, increment this value with :

 spawnTimer += Time.deltaTime;

Now the timer goes up in seconds. You can check this by adding

 Debug.Log( "spawnTimer = " + spawnTimer );

Then, if you want to reset the timer, use

 spawnTimer = 0.0;

That's about it =]

search google to read more, or see how other people are implementing timers : http://www.google.com/search?btnG=1&pws=0&q=unity+timer

avatar image Mz3D · Jul 30, 2012 at 01:28 PM 0
Share

Thank you very much! what does the % mean?

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

6 People are following this question.

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

Related Questions

Instantiating from an object that's in an array 1 Answer

What is the best way to instatiate an array of connected GameObjects? 0 Answers

using destroy with an array of GameObjects (C#) 1 Answer

Instantiating gameobject variables (in an array) from a different script 1 Answer

Instantiate large block maps 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