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 Esterelle · Jun 13, 2013 at 09:57 AM · c#timegenerator

[Solved] Making an object generator that generates according to time.

I tried making an object generator that generates objects according to time, but it's not working the way I want it to. I want the generator to generate only one object during certain time. Currently, my generator is working, just that it generates a lot of object continuously. I have a generator manager in which I will pass in float values and store them into an array and I will check them against the current time, if both values equates, the generator will generate one object. The result that I am getting now is when the value stored in the first array slot is equivalent to the value of the current time, nothing happens. When the value stored in the second array slot is equivalent to the value of the current time, the generator generates a lot of objects for a brief period of time. Can anyone kindly teach me what should I do to get the generator to generate one object at a time? Help rendered will be greatly appreciated!

Generator.cs:

 void Update ()
 {
     StateChange();
 }
     
 private void StateChange()
 {
     Camera mainCamera = Camera.main;
     GeneratorManager genManager = mainCamera.GetComponent<GeneratorManager>();
             
     if ( currentState == GeneratorState.active )
     {
         Spawn();
                 
     }
             
     else if ( currentState == GeneratorState.notActive )
     {
     }
    }
         
 private void Spawn()
 {
    // Instantiate the prefab
     Transform newPrefab = ( Transform ) Instantiate( thePrefab,
        new Vector3( this.transform.position.x, this.transform.position.y, this.transform.position.z ),
         Quaternion.identity);
 }

GeneratorManager.cs:

 void Update ()
 {
     Debug.Log ( Mathf.RoundToInt( Time.timeSinceLevelLoad ) );
     Generator gen = GameObject.Find("Generator").GetComponent<Generator>();
         
 for ( int i = 0; i < timeTable.Length; i ++ )
 {
     // If any of the component values inside the timeTable is the same
     // as the current time in game
     if ( timeTable[i] == Mathf.RoundToInt( Time.timeSinceLevelLoad ) )
     {
         isSpawn = true;
         Debug.Log ("Hey");
     }
             
     else
     {
         isSpawn = false;
     }
 }
         
 if ( isSpawn )
 {
     gen.SetIsActivated( true );
     theCounter ++;
             
     if ( theCounter >= timeTable.Length )
     {
         theCounter = 0;
     }
 }
         
 else
 {
     gen.SetIsActivated( false );
 }

}

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
0
Best Answer

Answer by Esterelle · Jun 13, 2013 at 11:27 AM

Generator.cs:

void Update () {
if ( Time.timeSinceLevelLoad >= timeTable[currentTimeTableIndex] ) { Spawn();

             if ( currentTimeTableIndex <= timeTable.Length - 1 )
             {
                 currentTimeTableIndex ++;
             }
         }
     }
 
     private void Spawn()
     {
         // Instantiate the prefab
         Transform newPrefab = ( Transform ) Instantiate( thePrefab,
             new Vector3( this.transform.position.x, this.transform.position.y, this.transform.position.z ),
             Quaternion.identity);
     }



This is the working solution.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fade in AudioLister over time #c 1 Answer

Flip over an object (smooth transition) 3 Answers

Spawning game Object after a period of time 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