• 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 ZLR_Games · Aug 05, 2020 at 11:19 PM · time.time

Why is my game not exporting correctly?

So I'm making a game where i have a spawner that only spawns certain prefabs, until a certain amount of time has passed. There are two times where it starts spawning more types of prefabs. Once after 5 seconds, and then when 30 seconds has passed, It unlocks even more things to be spawned. This works fine in the editor...but...when i export the game it doesn't work. When the 5 seconds pass, the new prefabs are spawned, but after 30 seconds...nothing. It just stays like it is. This is really annoying, cause i really need this to work, since these prefabs add a lot of variation to my game. (Don't want to give too many details)

So the way i actully do this is(example):

 public GameObject prefab1;
 public GameObject prefab2;
 public GameObject prefab3;
 public GameObject prefab4;
 public GameObject prefab5;
 public float rate = 2f;
 public float nextSpawn = 0f;
 public int whatToSpawn;

 public float narrowTime = 30f;
 public float movingTime = 5f;

 void Update()
 {
     if (Time.time >= nextSpawn)
     {
         if (Time.time >= narrowTime)
         {
             whatToSpawn = Random.Range(1, 6);

             switch (whatToSpawn)
             {
                 case 1:
                     Instantiate(prefab1, Transform.position, Quaternion.identity);
                     break;

                 case 2:
                     Instantiate(prefab2, Transform.position, Quaternion.identity);
                     break;

                 case 3:
                     Instantiate(prefab3, Transform.position, Quaternion.identity);
                     break;

                 case 4:
                     Instantiate(prefab4, Transform.positon, Quaternion.identity);
                     break;

                 case 5:
                     Instantiate(prefab5, Transform.position, Quaternion.Identity);
                     break;
             }


         }

         else if (Time.time >= movingTime)
         {
             whatToSpawn = Random.Range(1, 4);

             switch (whatToSpawn)
             {
                 case 1:
                     Instantiate(prefab1, Transform.position, Quaternion.identity);
                     break;

                 case 2:
                     Intsantiate(prefab2, Transform.position, Quaternion.identity);
                     break;

                 case 3:
                     Instantiate(prefab3, Tranform.position, Quaternion.identity);
                     break;
             }
         }

         else
         {
             whatToSpawn = Random.Range(1, 3);

             switch (whatToSpawn)
             {
                 case 1:
                     Instantiate(prefab1, Transform.position, Quaternion.identity);
                     break;

                 case 2:
                     Instantiate(prefab2, Transform.position, Quaternion.identity);
                     break;
             }
         }

             nextSpawn = Time.time + rate;
     }
 }

So basically i have a variable with the value of 5, and one with 30. I check if the in game time has reached, or gone above, that value yet. and i start by checking if the 30 seconds have passed, so the code in that if statement is the only code executed. This code has worked in previous builds (that are otherwise very outdated) but they were made in older versions of unity, so i don't know if that is the reason. It's just so specific, and i can't see why it would work after 5 seconds, but not 30 seconds. The only thing i can think of is using Time.deltaTime, but i don't see how that would work, since checking what the in game time is with Time.time should work (and has worked). I really hope you can help, cause i want this game released.

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 davidcox70 · Aug 05, 2020 at 11:29 PM 0
Share

Do you have value set in the editor for narrowTime, movingTime etc? These will overrule the ones in the script so it could be there are wrong numbers there. Time.time() is the correct one to use as it is the time in seconds since the game started.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

132 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 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 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 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 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 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

Reset time 1 Answer

Rate of fire c# with joystick. 1 Answer

Instantiate and time problem 1 Answer

How to reset Time.time ? 1 Answer

Getting current time from the worker thread 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges